Design a Load Balancer
medium 45 minSpread a shop's 100,000 requests a second over many servers.
Video lesson · Chapter 1 of 5
Three checkout workers share one address
A load balancer in 90 seconds
1:25Reel coming soon
Theory
Read or listen first. About 1 min.
What it is
A load balancer takes requests at one address and passes each to one of many servers. It stops sending work to servers that are sick, and it lets servers come and go unnoticed.
To design one, we build the data plane, which touches every request, and the control plane, which decides what the data plane knows.
Why it exists
One server has a ceiling and will fail one day. Several servers solve both, but only if something in front shares the work and notices failures.
That something now sees every request. If it is slow everything is slow, and if it stops everything stops. So the balancer needs the same care as the servers behind it.
A short history
1998
The Linux Virtual Server project begins: layer 4 balancing on ordinary Linux machines.
2002
The HAProxy site dates the project's use in high-traffic sites from this year.
2016
Google publishes Maglev, a layer 4 balancer spread over many ordinary machines.
August 2018
TLS 1.3 (RFC 8446) shortens a full handshake to one round trip.
April 2024
RFC 9568 restates VRRP, the protocol with which two machines share one address.
Words to know
- Virtual IP
- The one address clients use. It belongs to the service, not to a machine.
- Data plane
- The part that handles each packet or request.
- Control plane
- The part that decides pools, rules and weights and tells the data plane.
- Draining
- No new work for a server, while the work it has is finished.
- Slow start
- A returning server gets a small share that grows.
- Ejection
- Taking a server out because real requests to it fail.
- Direct server return
- Replies skip the layer 4 balancer.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Arrive. Packets reach one address and stay with one proxy for the life of the connection
Understand it
The controller does not fly the planes. It decides where the next one goes.
Scope
What are we building?
The balancing tier for a large shop: 100,000 requests a second at peak, HTTPS, several services behind one name. The basic ideas live in load balancing and layer 4 and layer 7. Here we put them together and size them.
Spread
Each request to one healthy server with room.
Notice
A failing server loses its traffic within a second or two.
Change
Deploy servers, rules and certificates without dropped requests.
Survive
Lose a proxy, a zone or a region.
Similar systems
| System | Kind | Known for |
|---|---|---|
| AWS ALB | Managed, layer 7 | Path rules, sign-in, cookies |
| AWS NLB | Managed, layer 4 | Static addresses, many connections |
| Cloudflare, Google Cloud | Managed, global | Balancing across regions |
| NGINX | Software, both | Web server and proxy |
| HAProxy | Software, both | Fast proxy with many checks |
| Envoy | Software, both | Service mesh, rich metrics |
| Traefik | Software, layer 7 | Finds services by itself |
| F5 BIG-IP | Appliance | Hardware in data centres |
The finished system
Where the design ends up
This is the last version, in one region. The Design tab starts with one server and one database.
One region of the system
Where to read what
| Question | Place |
|---|---|
| How do I open this interview? | Approach |
| How big is it? | Numbers |
| How does it grow? | Design |
| Health, failover, TLS? | Deep dives |
| Can I practise it? | Mock |
Summary
The whole topic in one minute.
A load balancer is a small decision made very often: which healthy server takes this request? Proxies decide alone, using their own counts and a copy of the configuration.
The design grows because of failure. Several proxies, layer 4 in front so that a dead proxy is bypassed, ejection in a second, draining and slow start, versioned configuration and a second region each remove one named break.
- Size for the failure: what is left after losing a zone must carry the peak.
- Layer 4 keeps one address alive; layer 7 makes the clever choice.
- Proxies count their own requests. Sharing counts costs more than it gives.
- Probes find dead servers in many seconds; real failures find them in one.
- When most servers look sick, distrust the checks and spread the load.