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.
See it run
Who gets the new version, how to go back, and how to keep a failing part from spreading.
Operations
Canary in steps
5%, 25%, 50%, 100%. After each step the new version is compared with the old one.
Step 1 of 6
Installed, no traffic
- Traffic on the new version
- 0%
Deployed, not released
Version 2 runs and nobody uses it.
Step 1: Installed, no traffic
for w in (5, 25, 50, 100): set_weight(w) wait_until(requests(new) >= 3000) if errors(new) > max(2 * errors(old), 0.005): set_weight(0) raise Rollbackkeep(old, hours=24)- Way back
- Seconds
- Costs
- Few servers
- All at once
- 20 x the users hurt
Wait first, then judge. Judging directly after a step looks at the traffic of the step before.
In the real world
The same advanced load balancing patterns doing real jobs. Pick one and step through it.
1,000 requests a second. Version 2 fails one request in five and is found after five minutes.
Step 1 of 4
To everybody
failed requests
60,000
And nothing to go back to
1,000 x 300 s x 20% = 60,000 failed requests.
Step 1: To everybody
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Judging before waiting
majorRight after a step the numbers belong to the step before.
Wrongset_weight(25)if errors(new) > 0.05: rollback()sleep(60)Rightset_weight(25)wait_until(requests(new) >= 3000)if errors(new) > 2 * errors(old): rollback()Choosing at random per request
majorA user with 20 pages flips between versions.
Wrongnew = random() < 0.05Rightnew = hash(user, salt) % 10_000 < 500A schema change in the switch
criticalThe old version cannot read the data any more, so there is no way back.
Wrongswitch(green)rename_column("name", "title")Rightadd_column("title") # expandswitch(green)drop_column("name") # weeks laterRetrying in every layer
criticalThree layers with three retries make 64 calls out of one.
Wrongapp.retry = proxy.retry = client.retry = 3Rightproxy.retry = 2proxy.retry_budget = "20%"
Interview tips
How to explain it
- Install first, move traffic later.
- Compare new with old.
- Going back is a number.
Memory trick: SAFE
- Small canary first.
- Automatic rollback, Fast fallback.
- Exponential backoff.
Choose
- Traffic to measure: canary.
- Little traffic: blue-green.
- Risky reads: mirror first.
Limits everywhere
- Ejected servers.
- Retries.
- Waiting time.
- Users on a trial.