Lab · Reflex

Be the load balancer

Seventy seconds of traffic, four backends, and every routing decision is yours. Requests come in different sizes, one server is smaller than the rest, and partway through another one slows down without failing a health check. Afterwards the same traffic is replayed through the algorithms real balancers use.

00:00/ 01:10idle
Routed
0req
p99 latency
0ms
Within SLO
100%
Timeouts
0
Incoming0 waiting
—

smallmediumheavy

Seventy seconds · four backends

You are the load balancer.

  • Send the oldest waiting request to a backend with keys 1–4, or tap a server. Each serves one request at a time, first in, first out.
  • Bigger requests take longer. srv-3 is half the size of the others. Requests left waiting 250 ms time out.
  • The SLO is 300 ms from arrival to response. Traffic picks up as you go, and the health checks will not tell you everything.

What the simulation models

Each backend is a first-in, first-out queue that works on one request at a time. Three serve 2.4 units of work a second; srv-3 is a smaller instance at half that. Requests cost one unit, two and a half, or six, in roughly a 70 / 24 / 6 split. Arrivals are a Poisson process whose rate climbs over the run, wobbles, dips once and bursts twice. On screen a second of game time is shown as 100 ms.

A request's latency runs from the moment it arrives to the moment its backend finishes it, so time spent waiting for you to decide counts. That is the first lesson: a real balancer decides in microseconds, and the replay gives the algorithms that advantage.

Why round-robin struggles

Round-robin gives every backend the same number of requests. That is only fair if every request costs the same and every backend is the same size, and neither is true here. srv-3 gets a quarter of the traffic with half the capacity, so its queue grows whenever load is high. A heavy request behind a light one makes the light one wait, and round-robin has no way to notice.

Least connections (join the shortest queue) routes to whichever backend has the fewest requests in flight. It doesn't know sizes or speeds either, but a slow or overloaded server keeps requests longer, its count rises, and it stops being picked. Load information turns into back-pressure without anyone configuring weights.

Power of two choices samples two backends at random and takes the less busy one. With four servers it does about as well as checking all of them, which is the point of it: at hundreds of backends, or with many balancers each holding a slightly stale view, comparing two is cheap and avoids every balancer piling onto the same “least loaded” server at once. The theory says two random choices cut the worst queue length from logarithmic to doubly logarithmic, and one extra sample buys almost all of the improvement.

The last row is an oracle that knows each request's cost and each server's true speed, and sends work to whichever will finish it first. No balancer has that information. It is there to show how much is left on the table.

Gray failure

Partway through, one full-size backend drops to a quarter of its speed for sixteen seconds. Its health check keeps passing, because the process is up and answering. This is the failure mode that hurts most in practice: a disk going bad, a noisy neighbour, a long garbage-collection pause, a dependency only that host talks to. A binary health check sees nothing wrong, and anything that keeps sending its fair share of traffic there keeps making it worse.

  • Watch latency, not liveness. The only signs are the ones on the cards: a growing queue and a recent latency well above its peers.
  • Outlier detection. Envoy and most service meshes can eject a backend whose error rate or latency is far from the rest of the pool, for a while, without waiting for a health check to fail.
  • Load-aware balancing. Least connections and power of two choices route around a slow server on their own: its queue grows, so it stops being picked until it drains.