Load balancing for system design interviews: why round robin fails, and what to use instead

2026-12-01来源 dev.toEnglish
Load balancing for system design interviews: why round robin fails, and what to use instead

The interview question: "You have four app servers behind a load balancer, and one of them is bigger than the others. How should the balancer decide where each request goes?"

"Round robin" is the answer most people give, and it is the one that quietly fails under real traffic. Interviewers want to hear how each algorithm behaves when requests cost different amounts, when one server gets slow, and when one dies. Here is the walkthrough, round by round, with numbers from the simulation in the Designbench load balancing lesson.

Round 1: The setup

Interviewer: Give me the numbers you're working with.

You: Four servers. S1, S2 and S3 each have 2 workers; S4 is bigger, with 4. A request takes 100 ms on average, so each worker handles 10 requests a second and the whole pool about 100. We send 70 requests a second, so the pool is 70% busy on average. Average busy is fine. What hurts is when one server is much busier than the others, because queues grow there first and latency explodes.

Round 2: Five algorithms, one run

Interviewer: Compare the common algorithms.

You: Same 60 seconds of traffic through each one:

  • Round robin: p50 148 ms, p99 861 ms. Busiest server S2 at 89%.
  • Weighted round robin: p50 116 ms, p99 621 ms. Busiest S3 at 76%.
  • Least connections: p50 80 ms, p99 501 ms. Busiest S3 at 77%.
  • Power of two choices: p50 97 ms, p99 511 ms. Busiest S3 at 79%.
  • Hash by user: p50 189 ms, p99 1,952 ms, and 4 requests rejected with a full queue. Busiest S1 at 95%.

The load balancing comparison on Designbench
Same 60 seconds of traffic through every algorithm on Designbench.

Why the spread? Round robin gives every server a quarter of the traffic: 17.5 requests a second each. The small servers can only do 20, so they run near 90% while the big one idles. Weighted round robin fixes known size differences by sending S4 twice as much. Least connections and power of two choices react to what is actually happening, so they also absorb requests that happen to be slow.

Round 3: Power of two choices

Interviewer: Explain power of two choices.

You: Pick two servers at random and send the request to the one with fewer requests in flight. It sounds too simple, but it gets most of the benefit of least connections without every balancer needing a perfectly up-to-date global view, which matters when you run many balancers in parallel.

Server pick(List<Server> healthy) { ThreadLocalRandom rnd = ThreadLocalRandom.current(); Server a = healthy.get(rnd.nextInt(healthy.size())); Server b = healthy.get(rnd.nextInt(healthy.size())); return a.inFlight() <= b.inFlight() ? a : b; // fewer requests in progress wins } // around each request Server s = pick(healthyServers()); s.inFlight.incrementAndGet(); try { return s.send(request); } finally { s.inFlight.decrementAndGet(); } 

The two picks can occasionally be the same server; that just means one choice that round, which is harmless.

Round 4: When a server dies or slows down

Interviewer: What if S2 dies?

You: This is where least connections has a trap. A dead server that fails requests instantly always has zero requests in progress, so least connections sends it more traffic, and every one of those requests fails. It is a black hole until something removes it.

So the algorithm is never enough on its own. You need:

  • Active health checks: the balancer calls a /health endpoint every few seconds and removes a server after a few failures in a row.
  • Passive checks (outlier detection): watch real responses, and eject a server that returns a burst of 5xx errors or is far slower than its peers, then let it back in gradually.
  • Retries on another server for idempotent requests, so the user never sees the failure. See Timeouts, retries and idempotency.

A slow server is harder to catch than a dead one. Least connections and power of two choices handle it naturally, because a slow server's requests stay in progress longer, so it looks busy and gets fewer new ones. Round robin keeps feeding it.

Round 5: Hashing and sticky sessions

Interviewer: When would you hash by user?

You: Only when the server holds state for that user, such as a local cache or a WebSocket connection. The cost shows in the run above: hashing ignores load, so one unlucky server with a few heavy users runs hot (S1 at 95%, p99 near 2 seconds). Prefer stateless servers with sessions in Redis or a signed token, so any server can take any request. If you must route by key, use consistent hashing so adding a server doesn't reshuffle everyone.

Round 6: The box in the middle

Interviewer: Isn't the load balancer itself a single point of failure?

You: Yes, so run at least two. A managed balancer (AWS ALB, Google Cloud Load Balancing) does this for you across zones. Self-managed, you pair them with a floating IP or put several behind DNS or anycast. Two more choices to state:

  • Layer 4 or layer 7: L4 balances TCP connections and is very fast but can't see HTTP. L7 understands requests, so it can route by path, terminate TLS, retry and balance per request rather than per connection. For a web API, L7 is the default.
  • Long-lived connections: with WebSockets or gRPC streams, balancing happens once per connection, so a new server gets no traffic until clients reconnect. Plan for gradual rebalancing.

Say it in the interview

"I'd put identical, stateless servers behind a redundant layer 7 balancer. Because request costs vary, I'd use least connections or power of two choices rather than round robin, which in my numbers pushed the small servers to about 90% busy and tripled p99. Health checks plus outlier detection remove dead or slow servers fast, and idempotent requests retry on another server. Sessions live in a shared store so any server can serve anyone."

Common mistakes

  • Choosing round robin with servers of different sizes or requests of different costs.
  • Forgetting health checks, or relying on least connections to avoid a broken server.
  • Sticky sessions by default, which pin load and break when a server dies.
  • A single load balancer with no redundancy.
  • Ignoring long-lived connections, which don't rebalance on their own.
  • Talking about average utilisation instead of the busiest server and p99.

Practise it

Kill a server, slow one down and run all five algorithms side by side in the load balancing lesson on Designbench. Related: How a request flows, Scaling: up or out and Circuit breakers.

原文:https://www.a8q9.cn/en/article/load-balancing-for-system-design-interviews-why-round-robin-fails-and-what-to-us-ba02d2.html

相关文章