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.
| Method | Returns / 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
- Round-robin: s1, s2, s3, then s1 again. After markUnhealthy("s1"), the next pick is s2.
- Weighted: over many calls about 50%, 30% and 20%. With s1 unhealthy, s2 and s3 split the traffic 3 : 2.
- Least connections: s2 has 2 connections and s3 has 1, so s3 is picked. s1 has none but is unhealthy.
2. Lessons to read before the load balancer
- 01.03 Redis for LLD: atomic counters, hashes and sorted sets, and scripts for anything that reads, decides and writes.
- 01.02 Thread-safety: concurrent
getNextServercalls must not hand out the same round-robin slot twice.
3. Load balancer design decisions to make first
- 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?
- 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.
- What does "proportional to weight" mean when a server is unhealthy? The weights of the remaining healthy servers decide the split.
- How does least-connections find the minimum quickly, and what happens on a tie?
- What does
totalRequestscount? The contract asks you to track it but doesn't say when it goes up. Pick a definition and keep it consistent. - What do the per-server calls do for an unknown id?
recordConnection,releaseConnection,markHealthyandmarkUnhealthycan all be called with an id that was never added or was removed. And what doesaddServerkeep (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
getNextServerthat 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:
releaseConnectionon 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:
weightedandrandomare checked by their distribution over many calls, not by an exact sequence.
Before you submit
getNextServerreturnsnullwith no servers, or when all are unhealthy- Round-robin visits healthy servers in the order they were added, and wraps
- Unhealthy servers are never picked;
markHealthybrings them back weightedpicks servers in proportion to their weights, over healthy servers onlyleast-connectionspicks a healthy server with the fewest active connectionsrandomonly returns healthy, registered serversaddServeron an existing id updates its weight without adding a second entryremoveServerremoves the server from routing, stats andgetActiveServers- Connection counts never go below 0, and stay exact under concurrent calls
getServerStatshas the four keys, and an empty map for an unknown server- All state is in Redis, and calls for unknown ids don't throw
Further reading
- HTTP load balancing methods (nginx docs)
INCR(Redis docs)- Hashes (Redis docs)
EVAL: atomic Lua scripts (Redis docs)
After you've tried it
Submit first. The full walkthrough of one solution: Building a Load Balancer with Four Routing Algorithms.