A load balancer sits in front of a group of servers and gives them one address. Clients connect to it, and it passes each request on to one of the servers behind it. Add servers and the site gets faster without anyone changing a URL. Lose one and, if all goes well, nobody notices.
That small job hides real choices: which server gets the next request, how to tell a server is dead, what to do with users who were logged in to it, and how to restart servers without dropping anyone. This page shows each one happening.
In the labs after the walkthrough, every coloured chip is one request in a simulation running in your browser. The letter is the client that sent it and the number counts that client’s requests, so A12 is client A’s twelfth request. Follow one from its client, through the load balancer, into a server’s queue and out again, or click it to see its story. A red chip with a cross has failed.
00 The life of one request
Before the live labs, follow a single request from the client to a server and back, then watch what happens when a server dies. Step through at your own pace, or press Play all.
Step 1 of 10
Meet the pieces
Clients send requests. The load balancer has the one public address they all use, and it decides which server gets each request. The servers behind it do the actual work. Clients never see the servers directly.
01 One front door, many servers
One server can only do so much. Each server here works on two requests at a time, at about 0.6 seconds each, so it handles a little over three a second. Beyond that, requests wait in its queue.
Put three servers behind a load balancer and the clients still see one address. They do not know or care how many servers there are, which means you can add, remove and replace servers freely.
- Served
- 0
- Longest queue
- 0
- Oldest waiting
- 0.0s
Click any chip to follow that one request.
A load balancer turns “make the server faster” into “add another server”. That only works if any server can handle any request, which sections 06 and 08 come back to.
02 Round robin and the slow server
Round robin is the simplest rule: server 1, then 2, then 3, then 1 again. It is fair in turns, and it is the default in many load balancers.
Fair turns are not fair work. When one server is slower, because of a noisy neighbour, a bad disk or a long garbage collection pause, round robin keeps giving it a third of the traffic. Its queue grows and every third request waits behind it.
Least connections sends each request to the server with the fewest requests open. The load balancer already knows that number, because the requests pass through it. A slow server holds its requests longer, so its count stays high and it gets fewer new ones.
- Longest queue
- 0
- Oldest waiting
- 0.0s
- Served
- 0
Click any chip to follow that one request.
Round robin is fine when servers and requests are alike. When they are not, pick an algorithm that looks at load.
03 Weighted: bigger servers get more
Sometimes servers differ on purpose: a new machine with three times the cores next to two old ones. Weighted round robin gives each server a weight and hands out turns in that ratio. With weights 3, 1 and 1, server 1 gets three requests in every five, spread out rather than in a clump.
Weights are also how you send a little traffic to a new version (a canary): give it weight 1 next to servers with weight 19, and it sees about 5% of requests.
- Average time
- 0.0s
- Longest queue
- 0
- Served
- 0
Click any chip to follow that one request.
Weights are something you set and must keep true. Least connections finds the same balance on its own, which is why many teams use it instead.
04 Random, and the power of two choices
Picking a server at random sounds lazy, and on average it is fair. But averages hide streaks. Every so often, chance sends several requests in a row to the same server, and with the servers this busy, its queue takes a while to recover.
The power of two choices fixes that cheaply: pick two servers at random and send the request to the one with fewer open requests. That small comparison removes nearly all the streaks. It matters most when there are many load balancers, each with its own slightly stale view of the servers: they would all rush to the same “least loaded” server, while random pairs spread them out. Envoy and NGINX both offer it.
- Longest queue
- 0
- Oldest waiting
- 0.0s
- Average time
- 0.0s
Click any chip to follow that one request.
Two random choices and a comparison give nearly the result of checking every server, at a fraction of the cost.
05 Health checks: noticing a dead server
The load balancer cannot see inside a server. To know it is alive, it asks: every few seconds it sends a health check, such as GET /healthz, and expects a quick 200. One failed check could be a blip, so a server is only removed after several failures in a row, and only added back after several passes.
That means a dead server keeps getting traffic for a while. The gap is roughly the check interval times the failure threshold, and every request sent in that gap fails.
- Failed
- 0
- Time to detect
- –
- Served
- 0
Click any chip to follow that one request.
Fast checks catch failures sooner but cost traffic and can flap on one slow reply. Many load balancers also watch real traffic (passive checks or outlier detection) and eject a server that starts returning errors.
06 Sticky sessions or stateless servers
Suppose each server keeps logged-in users in its own memory. Then a user who logs in on server 1 and whose next request lands on server 2 is a stranger there: logged out. One fix is sticky sessions: the load balancer remembers which server each client used (usually with a cookie) and keeps sending it there.
Stickiness hides the problem rather than solving it. When that server dies, every session on it dies too. It also makes load uneven, because a busy client cannot be spread out. The lasting fix is stateless servers: keep sessions in a shared store such as Redis or a database, or in a signed cookie, so any server can serve any request.
- Logged out
- 0
- Failed
- 0
- Served
- 0
Click any chip to follow that one request.
Use stickiness as an optimisation (warm caches), not for correctness. If losing one server logs people out, the state is in the wrong place.
07 Draining during a deploy
A rolling deploy restarts servers one at a time, so the others carry the traffic. Restart a server straight away and you cut off the requests it was working on, and the load balancer keeps sending it new ones until health checks notice it is gone.
Connection draining does it in order: tell the load balancer to stop sending new requests to the server, wait for its open requests to finish (with a time limit), then restart it, then add it back once it passes its checks. AWS calls the wait the deregistration delay; it defaults to 300 seconds.
- Cut off mid-way
- 0
- Failed
- 0
- Served
- 0
Click any chip to follow that one request.
Set the drain time a little above your slowest normal request. Long-lived connections such as WebSockets need the client to reconnect, so make reconnects routine.
08 Layer 4 or layer 7
The names come from the network layers. A layer 4 load balancer works with TCP (or UDP) connections. It sees addresses and ports, picks a server when a connection opens, and then copies bytes both ways. It is fast and works for any protocol, but it cannot see a URL, a header or a cookie, especially once the traffic is encrypted.
A layer 7 load balancer speaks HTTP. It ends the TLS connection, reads each request and then chooses: by path (/api to one pool, /img to another), by header, by cookie for stickiness. It can retry a failed request, add headers and pick a server per request rather than per connection.
That last point matters more than it sounds. With keep-alive, HTTP/2, gRPC and WebSockets, one connection can carry thousands of requests. A layer 4 balancer can only balance connections, so a busy client lands all its work on one server.
- Longest queue
- 0
- Oldest waiting
- 0.0s
- Served
- 0
Click any chip to follow that one request.
Waiting for a request…Bytes after the handshake are opaque. It picks a server once per connection.A common setup uses both: a layer 4 balancer at the edge for raw speed, in front of layer 7 proxies that do the routing.
09 Hot spots
Some routing pins work to a server on purpose. Hashing by client, user or key sends the same client to the same server every time, which keeps caches warm and needs no table of who went where. Consistent hashing, a careful version of this, is how caches and databases split data across nodes.
The catch is that load is rarely even across clients. One big customer, one celebrity account or one viral item can send more traffic than a single server can take. Hashing cannot split it, and adding servers does not help: the hot key still lands on one of them.
- Longest queue
- 0
- Oldest waiting
- 0.0s
- Served
- 0
Click any chip to follow that one request.
When one key is hot, spread that key: route it per request, give it its own pool, or split it (add a suffix to the key so it lands on several servers). Watch the busiest server, not the average.
10 The load balancer can fail too
Everything so far assumed the load balancer itself never fails. It is one box in front of everything, so if it goes down, every request fails no matter how many servers are healthy. Three common ways around that:
- An active and standby pair that share one address. They send each other heartbeats, and when the active one goes quiet the standby claims the address (keepalived and VRRP work like this). Failover takes about a second.
- DNS with several addresses, one per load balancer. Clients spread across them, and a dead one is removed from DNS. That is slow to take effect, because clients and resolvers cache answers for the record’s TTL, often longer.
- Anycast: the same IP address is announced from many places at once, and the internet’s routing sends each client to a nearby one. If a site goes away, routes move to the next. Cloudflare and Google’s front ends work like this.
Managed load balancers such as AWS ALB do this for you: the name points to a fleet of nodes across zones that scales and replaces itself.
- Failed
- 0
- Served
- 0
Click any chip to follow that one request.
Any part that every request passes through needs a spare. Check your design for the one box with no second.
11 Choosing a load balancer
The ideas above show up in every product under different names. A rough map:
| Product | Layer | Algorithms | Health checks | Good for |
|---|---|---|---|---|
| NGINX | 7, and 4 with the stream module | Round robin, weighted, least connections, hash, random with two choices | Passive in the open source version; active in NGINX Plus | A web server and reverse proxy you run yourself |
| HAProxy | 4 and 7 | Round robin, least connections, source and URI hashing, and more | Active and passive; slow start; drain by maintenance mode | A fast, very configurable proxy you run yourself |
| AWS ALB | 7 | Round robin, least outstanding requests, weighted random | Per target group; deregistration delay for draining | HTTP services on AWS, path and host routing |
| AWS NLB | 4 | Flow hash: one target per connection | Per target group | TCP and UDP at very high volume, static IPs |
| Envoy | 4 and 7 | Round robin, least request (two choices by default), ring hash, Maglev, random | Active checks plus outlier detection | Service meshes and gRPC, configured by an API |
| Cloudflare Load Balancing | 7 (proxied) or DNS | Weighted pools; steering by location or latency | Monitors from many locations | Spreading traffic across regions or clouds, on an anycast network |
Check the current docs before you choose: these products add features every year, and names and defaults change.
12 Check yourself
Three servers sit behind round robin. One of them is running a slow disk and takes four times as long per request. What happens?
Health checks run every 5 seconds and a server is removed after 3 failures in a row. It dies. Roughly how long are requests sent to it?
Your app keeps login sessions in each server’s memory and uses sticky sessions. One server crashes. What do its users see?
During a deploy you restart each server straight away. Users see a few errors on every deploy. What is the usual fix?
Clients keep one long-lived connection open and send many requests over it. A layer 4 balancer is in front. Why is load uneven?