Load balancer race
A load balancer has one job: decide which backend gets the next request. The simple rules work well when every backend is the same. They fall apart the moment one is slower than the rest, and in real fleets one always is: a noisy neighbour, a cold cache, a garbage collection pause.
Least connections keeps p99 latency to 1.91 s; Round robin reaches 1.5 min.
Requests waiting or in service, per backend
Bar height uses one scale for every algorithm: the tallest bar is 240 requests.
Backends and traffic
The last backend serves this share of a healthy backend’s speed. 100% means every backend is healthy.
Arrival rate as a share of the total capacity of all backends.
The same seed gives the same request stream to every algorithm.
Every algorithm on the same requests
| Algorithm | p50 | p99 | To slow node |
|---|---|---|---|
| Round robinTakes the backends in turn and ignores how busy they are. | 229.3 ms | 1.5 min | 25% |
| RandomPicks any backend at random. | 280.5 ms | 1.4 min | 23.6% |
| Least connectionsLowest p99Sends each request to the backend with the fewest requests in progress. | 150.4 ms | 1.91 s | 8.9% |
| Power of two choicesPicks two backends at random and uses the less busy one. | 192.2 ms | 3.04 s | 8.8% |
| Weighted round robinTakes turns in proportion to fixed weights, set here from each backend’s speed. | 222.3 ms | 2.98 s | 8.6% |
The slow backend can serve about 7.4% of the traffic. Round robin sent it 25%, so its queue is 240 requests deep and still growing; every request stuck behind it waits. Least connections sent it 8.9%: a backend that falls behind has more requests in progress, so it is picked less. Its queue is 3. Power of two choices gets most of the benefit by looking at only two backends per request, and sent it 8.8%. Weighted round robin also protects the slow node, but only because we gave it the speeds in advance.
What it shows
This simulation sends the same random stream of requests to five algorithms at once. Each backend is a queue that serves one request at a time. You can slow down one backend and watch what each algorithm does about it.
Round robin and random never look at how busy a backend is, so they keep feeding the slow one its full share. Least connections counts requests in progress. Power of two choices looks at only two backends, picked at random. Weighted round robin does not look at all, but is told each backend’s speed in advance.
How to read it
- Press Play to run a minute of traffic, or step one second at a time. The page opens on the finished run.
- In the table, p50 is the typical request and p99 the slowest in a hundred. Mid-run it covers requests finished so far; at the end it covers all, including the backlog.
- The last column is the share of requests sent to the slow backend. Its fair share is its speed divided by the total speed, so a backend at 25% speed among three healthy ones should get about 7%.
- Each bar chart shows requests waiting or in service on each backend. All five use one scale, and the slow backend is hatched and labelled “slow”.
- Backends is the number of servers, from 2 to 12. Load is the arrival rate as a share of total capacity. Seed picks the random stream; change it to check a result is not a fluke.
Questions
Why does least connections handle a slow server well?
A slow server finishes requests more slowly, so it has more requests in progress. Least connections reads that as a busy server and sends new requests elsewhere, with no need to know how fast anything is. It needs a count of in-flight requests per backend, which the load balancer already has.
How can picking from just two random backends work so well?
Mitzenmacher showed that when each request chooses the less loaded of two random servers, the busiest server’s queue grows far more slowly than with one random choice: roughly ln ln n instead of ln n ÷ ln ln n for n servers. A second look is almost all of the benefit. It also needs no global view of the fleet, which is why it suits large and distributed load balancers.
When is round robin the right choice?
When backends are the same size and requests cost about the same. It keeps no state, adds almost no overhead and is easy to reason about. Set the slow backend to 100% and it sits within a factor of two of the cleverer algorithms here.
Does weighted round robin fix a slow server?
Only if the weights are right. Here they come from each backend’s speed, which the algorithm is told up front. A backend that slows down later, with weights unchanged, is overloaded just like round robin overloads it. Algorithms that watch queue length adapt without being told.
How realistic is this model?
Each backend is a single queue with random service times and requests arrive at random, the M/M/1 model. Real services have several workers, uneven request sizes and bursty traffic, which usually makes waiting worse. The shape of the result, a slow backend ruining the tail under oblivious routing, is what carries over.
Learn it properly
Sources
- Mitzenmacher, M. (2001). The power of two choices in randomized load balancing. IEEE Transactions on Parallel and Distributed Systems 12(10).
- NGINX documentation: ngx_http_upstream_module (round robin, weights, least_conn).
- HAProxy configuration manual: the balance keyword (roundrobin, leastconn, random).
Explore further
- Load balancingSam Rose’s interactive essay comparing load-balancing strategies.
Last updated
More tools
All tools- estimatorBack-of-envelope estimatorTurn daily users into requests per second, storage and bandwidth, with the working shown.Open tool
- simulatorConsistent hashing simulatorA hash ring with virtual nodes: key spread per node, and how many keys move when the cluster changes.Open tool
- estimatorQueue latency estimatorWhy latency explodes as servers get busy: utilisation, waiting time and Little’s law.Open tool