Design a Load Balancer
⚖️ What is a load balancer? It is the piece that stands in front of a group of identical servers and decides which one handles each request. Clients see one address; behind it, the balancer spreads the work so no single server is overloaded, skips servers that are down, and sends a failed request somewhere else.
In a low-level design interview you are not building the network proxy itself. You are designing the decision logic inside it: the classes that hold the list of backends, the rule that picks one, and what happens when a backend fails. Interviewers ask for it under several names, "round robin load balancer", "weighted load balancer", "least connections", "random balancer", "load balancer with retries", and they are all the same design. The picking rule changes; everything around it stays the same. We build one balancer with a swappable picking rule and cover every variant: four rules in the main design, and retries on another backend as a requirement.
Requirements
"Design a load balancer that distributes requests across a set of backend servers. It should support different balancing algorithms."
The interesting part is hidden in "different algorithms" and in what happens when things go wrong. The questions below turn the prompt into rules.
Clarifying Questions
You: Which algorithms do we need?
Interviewer: Round robin, weighted round robin, least connections and random. We pick one when the balancer is created. More will come later.
You: Is this the network proxy, with sockets and HTTP parsing, or the component that chooses the server?
Interviewer: Just the component that chooses. Assume the proxy calls you with a request id, you return a server id, and the proxy tells you when the request finishes or fails.
You: How do we know a server is down?
Interviewer: A separate health checker probes the servers and tells you when one goes up or down. Never send new requests to a server that is down.
You: What happens when a request fails on a server?
Interviewer: Try it on another server. Never send the same request to a server it already failed on. If every server has been tried, give up.
You: Can servers be added and removed while traffic is flowing?
Interviewer: Yes. We scale up and down all the time. If a server is removed, the requests still on it are lost; the proxy handles that.
You: What do the weights mean?
Interviewer: A server with weight 3 should get three times as many requests as one with weight 1 under weighted round robin. The other algorithms ignore weights.
You: Will many threads call the balancer at once?
Interviewer: Yes, eventually. Get it right for one thread first, then make it safe.
The fourth answer is the one people skip. "Retry on another server" sounds like one line of code, but it means the balancer must remember, per request, which servers it already tried. That is a new piece of state, and it shapes the classes.
💡 Interview tip: Ask what the balancer's inputs and outputs are before you draw any class. "A request id in, a server id out, and a callback when it ends" turns a vague networking question into a clean object design with three or four methods.
Final Requirements
- Servers can be added (with an id and a weight) and removed at any time. Ids are unique.
- A server is healthy or unhealthy. Only healthy servers receive new requests. A health checker changes the status from outside.
routepicks a server for a new request using the configured algorithm and records that the request is in flight on it.- The algorithm is one of round robin, weighted round robin, least connections and random, chosen at creation. Adding a new one must not change the balancer.
- Least connections needs to know how many requests each server is handling right now, so the balancer tracks open connections per server.
- When a request fails,
retrymoves it to another healthy server it has not tried yet, or gives up if there is none. - When a request finishes, its connection is closed.
- Removing a server drops the requests in flight on it.
Out of scope
- Sockets, HTTP and TLS: the proxy layer does this.
- Running the health probes (we only receive their results).
- Sticky sessions and consistent hashing (we come back to these in Extensibility).
- Balancing across data centers and the networked side of a balancer (see /system-design/load-balancer).