Why you need it
One server can answer only so many visitors at once. When more arrive, they wait, and then they leave. Buying more servers does not help by itself: something has to send visitors to the new ones.
Think of it like this
A supermarket with eight tills and one person at the front of the queue saying "you, till 3". Nobody has to pick a line, and no till sits empty while another has ten people waiting.
Where you’ll find it
- Google: Maglev, its own load balancer, sits in front of Search, Gmail and YouTube.
- GitHub: GLB Director spreads every page view and every
git pushacross its servers. - Cloudflare: Unimog shares traffic out inside each of its data centres worldwide.
- Most other apps: Use a ready-made one: AWS Elastic Load Balancing, NGINX or HAProxy.
What to know first
One address, many machines. Visitors only know one name, like shop.com. The load balancer owns that address; the servers behind it stay hidden.
Any server can answer anyone. This only works if servers do not keep your login or cart in their own memory. See Stateless services and sessions.
Open connection. A request that has started but not finished yet. A busy server has many of these at the same time.
How it works
- Visitors talk to the balancer, not the servers. Every request goes to the load balancer first. It picks one server, passes the request on, and hands the answer back.
- It picks a server with a simple rule. Round robin: take turns, one each. This is the one in the animation.Least connections: pick the server with the fewest open requests. Better when some requests are slow.Two random choices: pick two servers at random, use the less busy one. Cheap, and nearly as good as checking all of them.
- It skips servers that are dead. Every few seconds it asks each server "are you alive?". A server that stops answering is left out: it gets no more visitors until it recovers. See Health checks and heartbeats.
- It reads a little, or a lot. A layer 4 balancer only looks at addresses, so it is very fast; Maglev and GLB work this way. A layer 7 balancer reads the web request itself, so it can send
/apito one group of servers and/imagesto another; NGINX and AWS ALB work this way. - Run two of them. One balancer in front of everything is a new Single points of failure. Real systems run at least two, so one can die and the other keeps going.
An example
Each server can answer 100 requests a second. On launch day, 300 a second arrive.
With one server: 100 are answered, 200 wait, and most of those give up.
With three servers behind round robin: 100 each. Everyone is answered.
Add a fourth: 75 each, so there is room for a spike. Now one dies: within a few seconds the health check notices, and the other three go back to 100 each. Nobody outside sees a thing.
Further reading
- Load Balancing, Sam Rose. Interactive. Watch round robin, least connections and others race each other. Start with this one.
- The power of two random choices, Marc Brooker. A short post on why picking the better of two random servers works so well.
- Using nginx as HTTP load balancer, NGINX docs. One page. See what a real setup looks like in ten lines of config.
- Load Balancing in the Datacenter, Google SRE book. How Google shares work between thousands of servers. Free to read.
- Maglev: a fast and reliable software network load balancer, Google, NSDI 2016. The paper behind Google's balancer, for when you want the deep end.