AlgoViz

Advanced load balancing patterns

medium 30 min

A 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

Our systemUsersLoad balancerweightsStable v1100%Canary v20%
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

Our systemUsersLoad balancerVersion 1removedVersion 2100%, failing

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

    major

    Right after a step the numbers belong to the step before.

    Wrong
    set_weight(25)if errors(new) > 0.05: rollback()sleep(60)
    Right
    set_weight(25)wait_until(requests(new) >= 3000)if errors(new) > 2 * errors(old): rollback()
  • Choosing at random per request

    major

    A user with 20 pages flips between versions.

    Wrong
    new = random() < 0.05
    Right
    new = hash(user, salt) % 10_000 < 500
  • A schema change in the switch

    critical

    The old version cannot read the data any more, so there is no way back.

    Wrong
    switch(green)rename_column("name", "title")
    Right
    add_column("title")     # expandswitch(green)drop_column("name")     # weeks later
  • Retrying in every layer

    critical

    Three layers with three retries make 64 calls out of one.

    Wrong
    app.retry = proxy.retry = client.retry = 3
    Right
    proxy.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.