DEV Community

Cover image for Load balancing algorithms: round robin vs least conn
System Design Simple
System Design Simple

Posted on Originally published at systemdesignsimple.com

Load balancing algorithms: round robin vs least conn

A load balancer picks a server per request: take turns (round robin), fewest unfinished requests (least connections), or a hash of a key such as the user ID. When requests cost about the same, any of them works. When request costs vary, the choice matters a lot. In our 4-server simulation, quick requests had a p99 of 150.3 ms under round robin, 22 ms under least connections, and 468.9 ms under user-ID hash, for requests that needed only 20 ms of work.

That is the answer up front: with mixed request costs, a rule that looks at each server's unfinished work keeps quick requests quick. The rest is why.

Our simulation

Our discrete event simulation: 4 servers of 8 slots, 20,000 Poisson arrivals, 10.25% of requests slow at 400 ms, the rest 20 ms, 66.2% load. Chosen numbers, not a measurement of any real system.

Round robin sends requests in a fixed cycle and never looks at how busy a server is. A slow request occupies a slot, the next quick requests queue behind it, and the queue builds. Peak queue on any server reached 24, and the quick-request p99 hit 150.3 ms.

Least connections picks the server with the fewest open requests, so quick requests route around the busy servers. Peak queue was 2, and the quick-request p99 was 22 ms, barely above the 20 ms of work itself.

User-ID hash sends every request from one user to the same server, and user traffic is skewed: the top user sent 12.1% of all requests. The busiest server got 32.7% against a fair 25% share, its peak queue hit 71, and the quick-request p99 was 468.9 ms. Note the overall p99 hides most of this, 408.1 ms for round robin, 400 ms for least connections, 523.1 ms for hash, because the overall p99 is a slow 400 ms request. Measure quick requests separately.

The control run, and the details

Control run: every request 20 ms, 22.5% load, all three rules had a p99 of 20 ms. Hashing still sent 32.7% to one server, but at 29.4% load nobody noticed. Skew only hurts when queues can form.

What least connections counts depends on the software: NGINX least_conn counts active connections per worker process, unless the upstream has a shared memory zone; HAProxy leastconn compares current load per server. Hashing is still right when you need the same key on the same server, for a local cache or session state. Use consistent hashing, and protect it against hot keys.

Our take: no single best algorithm. Requests about the same cost and identical servers: round robin is simple and fair. Costs that vary a lot: use a rule that looks at unfinished work, such as least connections or power of two choices. Same key on the same server: hash, and watch the hot key.

Read the full article, with the NGINX and HAProxy details, health checks, slow start, and the full simulation setup, on System Design Simple: https://systemdesignsimple.com/which-server-gets-your-request/

Top comments (0)