Octet

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

AlgorithmGood forWatch out for
Round robinShort, uniform requestsIgnores server load
Weighted round robinMixed server sizesWeights need maintenance
Least connectionsWebSockets, database proxies, long sessionsA new server with zero connections gets flooded
Least response timeServers that degrade unevenlyNoisy measurements
Source IP hashSimple affinity without cookiesMany users behind one NAT all land on one server
Consistent hash (ring, Maglev)Caches, sharded backendsAdding a server moves only about 1/N of keys
Random of two choicesLarge pools, distributed balancersNeeds 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.

Traps

  • Recommending source IP persistence for public internet users behind carrier-grade NAT.
How did you do?