02. Scaling / Lesson 2 of 5
Load Balancing
One address for the users, many servers behind it.
- 10 min
- Beginner
- 11 steps
- Busiest server100% (bad)
- Healthy servers3/3
- Failed requests0
- Request
- Health check
- Failed request
Step 1 of 11: Three servers, but everyone uses one
Read every step as text
Step 1: Three servers, but everyone uses one
The shop got busy, so we bought two more servers. But the domain name still points at one address: the first server's.
Every browser sends its requests there. S1 is overloaded while S2 and S3 sit idle.
Adding servers does nothing until something spreads the requests over them.
Step 2: Put a load balancer in front
A load balancer takes over the public address. Users talk only to it; the servers sit behind it on private addresses.
For each request, the balancer picks a server and passes the request on. The answer comes back the same way.
Users see one address. Behind it you can add, remove or replace servers without telling anyone.
Step 3: Round robin: take turns
The simplest rule: request 1 to S1, request 2 to S2, request 3 to S3, then start again at S1.
It needs no information about the servers, and over many requests each one gets a third.
Round robin is a fine default when every request costs about the same.
Step 4: Equal requests are not equal work
Most requests take 20 ms, but a sales report takes 2 seconds. Round robin counts requests, not work.
When a few slow reports happen to land on S2, its queue grows while S1 and S3 have spare time. Users sent to S2 wait.
One slow request can cost as much as a hundred fast ones.
Step 5: Least connections: pick the least busy
The balancer already knows how many requests each server is working on. Least connections sends each new request to the server with the fewest.
S2 is busy with reports, so new requests go to S1 and S3 until S2 catches up.
Round robin looks at the order. Least connections looks at the servers.
Step 6: Weights for servers of different sizes
S3 is a new machine with twice the CPU. Give it weight 2 and the others weight 1, and it receives twice as many requests.
Weights work with round robin and with least connections (open requests divided by weight).
Weights also help a careful roll-out: give a new server weight 1 of 10 first, and raise it if all is well.
Step 7: Health checks: is each server alive?
Every few seconds the balancer calls a small address on each server, such as
GET /health. A healthy server answers200 OKquickly.If a server misses, say, 2 checks in a row, the balancer stops sending it traffic. When it passes again, it is put back.
A good health check tests what the server needs (its database connection), but stays cheap: it runs every few seconds.
Step 8: A server dies, and the balancer routes around it
S2 crashes. Until the balancer notices, the requests it sends there fail. After 2 missed checks, about 10 seconds here, S2 is taken out.
From then on S1 and S3 share all the traffic and users see no more errors. Shorter check intervals shrink the error window.
Keep enough spare capacity: two servers must now carry the load of three.
Step 9: Sticky sessions: same user, same server
Some apps keep a user's login or cart in the server's memory. Then each user must keep reaching the same server, or they look logged out.
The balancer can do this with a cookie: the first answer sets
srv=S1, and later requests with that cookie go to S1.Sticky sessions make the load uneven, and when that server dies its users lose their session. The better fix is the next lesson: stateless servers.
Step 10: L4 and L7: what the balancer can see
A layer 4 balancer works with TCP connections: it sees addresses and ports, not the request. It is very fast and works for any protocol.
A layer 7 balancer reads the HTTP request. It can send
/api/…to API servers and/img/…to image servers, end TLS once, and retry a failed request elsewhere.Many sites use both: an L4 balancer spreads connections over a few L7 balancers, which route by URL.
Step 11: Load balancing in one picture
Users reach one address; the balancer spreads requests over the servers. Round robin takes turns, least connections picks the least busy, and weights favour bigger machines.
Health checks remove a dead server within seconds. Keep servers stateless so any of them can answer any request.