What load-balancing algorithms do you know, and when would you use each?
Show the answer
Say this first
Round robin, the usual default, for short and similar requests. Weighted round robin when servers differ in size. Least connections, often weighted, for long-lived or uneven sessions. Least response time when servers slow down unevenly. Source IP hash or consistent hashing when the same client or key should land on the same server, as with caches. Persistence (stickiness) is a separate setting, by source IP or by cookie.
Go deeper
| Algorithm | Good for | Watch out for |
|---|---|---|
| Round robin | Short, uniform requests | Ignores server load |
| Weighted round robin | Mixed server sizes | Weights need maintenance |
| Least connections | WebSockets, database proxies, long sessions | A new server with zero connections gets flooded |
| Least response time | Servers that degrade unevenly | Noisy measurements |
| Source IP hash | Simple affinity without cookies | Many users behind one NAT all land on one server |
| Consistent hash (ring, Maglev) | Caches, sharded backends | Adding a server moves only about 1/N of keys |
| Random of two choices | Large pools, distributed balancers | Needs a load signal |
Persistence
- Source IP affinity: breaks behind carrier NAT and for mobile users who change IP.
- Cookie insert: the layer 7 balancer sets a cookie naming the server. The most reliable for HTTP.
Defaults
HAProxy uses balance roundrobin unless told otherwise. F5 pools default to round robin. Envoy's least
request picks two random hosts and sends to the less busy one.
They will ask next
How do you stop a freshly added server from being overwhelmed under least connections?
Slow start: the balancer ramps the new server's share up over a period, for example
slowstart 30son an HAProxy server line.
Traps
- Recommending source IP persistence for public internet users behind carrier-grade NAT.