Advanced load balancing patterns
medium 30 minA load balancer decides which server gets a request, so it can also decide which version gets it and what happens when a server misbehaves.
Video lesson · Chapter 1 of 5
A release meets its first fifty real requests per second
Theory
Read or listen first. About 2 min.
What it is
Spreading requests evenly is only the first use of a load balancer. Because every request passes through it, it can also send a chosen part of the traffic somewhere else. That one ability is behind most of the patterns on this page.
Two groups of patterns follow. Release patterns (canary, blue-green, mirroring) decide which version of the software gets the traffic. Protection patterns (ejecting bad servers, circuit breaking, timeouts, retries) decide what to do when something fails.
Why it exists
Most outages start with a change. If a new version goes to everybody at once, everybody finds the fault. If it goes to one user in twenty, the fault costs a twentieth.
The second cause is failure that spreads. One slow service makes its callers wait, they fill up and become slow too. A balancer that stops sending to the slow part, and refuses to repeat requests without limit, breaks that chain.
A short history
March 2010
Martin Fowler describes blue-green deployment: two environments and a switch between them.
March 2014
Fowler describes the circuit breaker, popularised by Michael Nygard's book Release It.
May 2014
Danilo Sato describes parallel change (expand and contract), which lets two versions share one database.
June 2014
Danilo Sato describes canary release on the same site.
October 2023
Gateway API 1.0 for Kubernetes: traffic weights are part of a standard interface.
Words to know
- Canary
- A small part of the traffic that gets the new version first.
- Control
- The part that keeps the old version, to compare with.
- Blue and green
- Two complete environments. One serves, the other waits or is updated.
- Mirroring
- Sending a copy of requests to a second version and ignoring its answers. Also called shadowing.
- Rollback
- Going back to the old version. With weights it means setting a number to 0.
- Outlier ejection
- Removing one server that behaves worse than the others, for a while.
- Retry budget
- A limit on retries as a share of normal requests.
- Expand and contract
- Change the database in steps so that the old and the new version both work.
Understand it
Start here
A new slide at the water park
The blue slide was never taken down while the green one was on trial.
| At the water park | In the system |
|---|---|
| Blue slide, green slide | Old and new version |
| Guard who sends visitors | Load balancer with weights |
| Guard who watches | Automatic comparison of errors and delay |
| Chain | Weight 0 |
| Slide kept standing | Old version kept for rollback |
The idea
Deploying is not releasing
Deploying puts a version on servers. Releasing sends users to it. When they are two steps, you can stop between them. The balancer is what separates them.
Four patterns, one question each
Canary
Is it safe for a few? Then for more.
Blue-green
Can we switch everybody, and back, in seconds?
Circuit breaker
Should we call it at all right now?
Retry with timeout
How long do we wait, and may we ask again?
A canary with a judge
Canary
Shift weight in steps and compare
A common plan
| Stage | Old version | New version | Wait for |
|---|---|---|---|
| 1 | 95% | 5% | First errors, crashes, memory |
| 2 | 75% | 25% | Delay under load |
| 3 | 50% | 50% | Database and cache effects |
| Done | 0% | 100% | Keep the old one for a while |
One step of a rollout
Who is hurt by a bad version
How many requests you must see
Choose users, not requests. With a random choice per request, a user who loads 20 pages meets a 5% canary with a chance of 1 - 0.95^20, about 64%, and flips between versions. A hash of the user's id keeps each user on one side, and a user who was in at 5% is still in at 25%.
from zlib import crc32def bucket(user, salt): return crc32(f"{salt}:{user}".encode()) % 10_000def version(user, salt, percent): return "new" if bucket(user, salt) < percent * 100 else "old"users = [f"user-{i}" for i in range(10_000)]at5 = {u for u in users if version(u, "r42", 5) == "new"}at25 = {u for u in users if version(u, "r42", 25) == "new"}print(len(at5), len(at25), at5 <= at25) # about 500, about 2500, TrueBlue-green
Two environments and a switch
Two complete production environments run at the same time. Blue serves, green is updated and tested. Then all new traffic goes to green and blue waits as the way back.
After the switch
Switch and back
Good at
- Going back in seconds.
- Testing the real environment before users come.
- No mix of two versions for web requests for long.
Hard at
- Everybody gets the fault at once.
- Two environments cost up to double.
- Open connections and cached DNS stay on the old side.
- Data written by version 2 that version 1 cannot read.
Mirroring
A copy for the new version
The balancer sends each request to the old version and a copy to the new one. The copy's answer is thrown away. Istio and nginx have this built in. You see errors, delay and load with real traffic and hurt nobody.
Mirrored traffic
Choose
Which release pattern?
| Rolling update | Canary | Blue-green | Mirroring | |
|---|---|---|---|---|
| Who meets a fault | More and more users | A few | Everybody, briefly | Nobody |
| Way back | Minutes | Seconds | Seconds | Not needed |
| Extra machines | Few | Few | Up to double | Up to double |
| Versions side by side | Yes | Yes, for longer | Only while draining | Yes |
| Needs | Nothing | Weights and good metrics | A second environment | Safe side effects |
Decide
Two relatives: header routing sends your own staff to the new version by a header or cookie, and A/B tests use the same split to ask which version users prefer. An A/B test answers a product question and runs for weeks. A canary answers a safety question and runs for minutes or hours.
Protection
Eject the bad server
Health checks ask now and then. Outlier ejection watches the real answers: after several server errors in a row, or a success rate far below the others, the server is out for a while and then tried again. This is a circuit breaker for one server, run by the balancer.
Circuit breaker states
The pattern has a page of its own: circuit breaker. Two facts belong here. A balancer must limit how many servers it ejects, or a shared fault removes all. And when too few are healthy, it is better to use all of them: Envoy calls this panic mode, at 50% by default.
Protection
Timeouts and retries with a budget
Exponential backoff, base 100 ms
| After failed attempt | Wait | Total waited |
|---|---|---|
| 1 | 100 ms | 100 ms |
| 2 | 200 ms | 300 ms |
| 3 | 400 ms | 700 ms |
| 4 | 800 ms | 1,500 ms |
Details are on the pages about timeouts and retries. At the balancer three rules matter:
- Retry on another server, not on the one that failed.
- Retry only what may happen twice: reads, or writes with an idempotency key.
- Retry in one layer, with a budget.
Retries multiply through layers
A budget allows retries only up to a share of normal requests, for example 20%. With 1,000 requests a second and everything failing, the load becomes 1,200. With 3 retries each it would be 4,000. Envoy recommends retry budgets over a fixed maximum.
Together
In which order the pieces act
- 1
Choose the version
Weights or a hash of the user: old or new.
- 2
Choose a server
Among healthy ones that are not ejected. See load balancing.
- 3
Send with a timeout
Never wait without a limit.
- 4
Maybe retry
On another server, if the budget allows and the request may happen twice.
- 5
Count
Errors and delay per version and per server. These numbers feed the canary and the ejection.
Summary
The whole topic in one minute.
Release patterns move traffic, not software: install first, then shift weight, compare with the old version, and go back by changing a number. Canary limits who is hurt, blue-green makes going back fast, mirroring hurts nobody but cannot show everything.
Protection patterns stop failure from spreading: take the bad server out, refuse early, wait for a limited time, retry little. Without limits retries become the outage.
- Deploying and releasing are two steps. Weights separate them.
- Canary: 1% to 10% first, compare with the old version, hash users, go back by setting 0.
- Blue-green: two environments, switch in seconds, database changes must fit both.
- Mirroring: real traffic, no answers used, beware of writes.
- Eject bad servers, but limit how many.
- Retries need backoff, jitter, another server and a budget.