LLD Guide · Module 2: Problem prep · Lesson 02.10

Prep: Load Balancer

Four routing algorithms over shared Redis state, health-aware, at ten thousand calls a second.

By Cruscible Engineering · Published

Each algorithm is a few lines on its own. The work is making four of them share one set of servers, skip the unhealthy ones, and stay correct when eight threads ask at once.

This lesson prepares you for Build a Load Balancer. It doesn't solve it.

1. What the load balancer contract asks for

A pool of servers, each with a weight, a health flag and a count of active connections, all persisted (Redis is recommended). getNextServer picks one healthy server by the named algorithm, or returns null when there is none.

MethodReturns / does
addServer(id, weight)adds a healthy server; for an existing id, updates its weight
removeServer(id)removes the server and all its state
getNextServer(algorithm)a healthy server id by round-robin, weighted, least-connections or random; null if none
recordConnection(id) / releaseConnection(id)active connections up or down; never below 0
getServerStats(id)activeConnections, totalRequests, weight, healthy; an empty map if unknown
getActiveServers()every registered id, healthy or not
markUnhealthy(id) / markHealthy(id)takes a server out of routing, or puts it back

Figure: Three servers with weights 5, 3 and 2: what each algorithm picks

2. Lessons to read before the load balancer

3. Load balancer design decisions to make first

  1. Where does the server order live? Round-robin cycles "in order", meaning the order servers were added. A Redis set has no order. What keeps it?
  2. How do concurrent round-robin calls each get the next server? Two threads reading the same position and both incrementing it is the race from 01.02.
  3. What does "proportional to weight" mean when a server is unhealthy? The weights of the remaining healthy servers decide the split.
  4. How does least-connections find the minimum quickly, and what happens on a tie?
  5. What does totalRequests count? The contract asks you to track it but doesn't say when it goes up. Pick a definition and keep it consistent.
  6. What do the per-server calls do for an unknown id? recordConnection, releaseConnection, markHealthy and markUnhealthy can all be called with an id that was never added or was removed. And what does addServer keep (connections, health) when it updates an existing server?

4. Load balancer pitfalls

⚠️ Trap: the performance target is 10,000 operations a second at a p99 of 5 ms. A getNextServer that makes one Redis call per server, per request, won't get there.

⚠️ Trap: calls for unknown servers do happen under load. They must not throw: an exception counts against your error rate.

⚠️ Trap: releaseConnection on a server with 0 connections leaves it at 0, never -1.

⚠️ Trap: no in-process state. A round-robin position in a Java field breaks "persist all server state".

💡 Tip: weighted and random are checked by their distribution over many calls, not by an exact sequence.

Before you submit

Further reading

After you've tried it

Submit first. The full walkthrough of one solution: Building a Load Balancer with Four Routing Algorithms.

Open the problem