ScalingIntermediate

Which load-balancing algorithm, and why?

“Compare round robin, least connections and consistent hashing. When does each fail, and what do health checks get wrong?”

What this tests

  • Matching the algorithm to request cost variance and cache affinity
  • Health check failure modes, including marking everything unhealthy
  • L4 vs L7 balancing
  • The balancer as a single point of failure

Answers by level

Read the beginner answer first and notice what is missing.

Round robin assumes requests cost the same and backends are identical. It fails when request cost varies — one instance stuck with three heavy reports while the next gets cheap health pings — or when instances differ in size (weighted round robin fixes the second). Least connections adapts to cost variance by sending to the instance with the fewest in-flight requests; it fails when connection count does not reflect load, such as long-lived idle connections, and it needs the balancer to see all traffic to count accurately.

Consistent hashing routes by a key (user id, cache key) so the same key lands on the same instance. The reason is affinity: an in-process cache or a WebSocket state that would be useless if requests scattered. It distributes less evenly, hot keys create hot instances, and virtual nodes are needed to smooth the ring; when an instance leaves, only its keys move.

Health checks decide who is in the pool. Active checks hit an endpoint every few seconds; passive checks watch real responses. The danger is a check that depends on a shared dependency — if the database is slow, every instance fails its check simultaneously and the balancer removes everyone, turning a slow database into a total outage. Checks should verify the instance, not the world, and balancers should refuse to empty the pool below a floor.

Green flags · Red flags

Strong green flag · Recommends health checks that test the instance rather than its dependencies, with a floor that prevents emptying the pool.
Green flags
  • Ties round robin to uniform cost, least connections to variance, hashing to affinity
  • Knows the "everything unhealthy" failure from dependency-based health checks
  • Distinguishes L4 from L7 and the HTTP/2 connection problem
  • Addresses the balancer as a SPOF
  • Mentions virtual nodes or bounded loads for hashing
Red flags
  • "Round robin is always fine; it spreads load evenly."
  • Uses a health check that queries the database
  • Does not know why consistent hashing would ever be chosen
  • Has one balancer with no redundancy

Follow-up questions

F1
The database slows down and suddenly all instances are out of the pool. Why?
F2
When is least connections wrong?
F3
Why consistent hashing instead of hash mod N?

Scenario

A 12-instance API behind round robin shows p99 of 3 s on two instances and 200 ms on the rest. Those two happen to receive most of the export requests, which run 20 s each. Choose an algorithm, explain what changes, and say what else you would fix about the exports.

Learn this topic