Networking / Load balancers

Load balancers

One address, many servers. Follow 240 requests a second through a load balancer to see how it picks a server, notices a dead one, and keeps a user on the same machine.

Beginner8 steps~8 min
Clients — send requests to one public address
Load balancer — picks a server for each request
Servers — A, B and C: identical copies of the site
// THE SHORT VERSION

In 30 seconds

A load balancer sits in front of a group of identical servers and hands each incoming request to one of them, so no single machine has to carry all the traffic or take the site down when it fails.

It can choose by taking turns (round robin), by picking the least busy server, or by hashing something about the request.

It checks regularly that each server is alive and skips those that are not. Some balancers see only addresses and ports; others read the request itself and can keep a user on one server.

Load balancers, step by step

STEPS01 / 08

The hash is a toy. Real balancers use stronger ones, often consistent hashing, so that adding or removing a server moves few clients. The balancer itself is normally run as a redundant pair.

STEP 01 / 08 One server is not enough
THE MATH
demand: 240 req/s capacity: 150 req/s backlog grows 90 per second
  1. Step 1: One server is not enough. One server can only do so much. Here 240 requests arrive each second and the machine can finish 150, so the rest queue up and pages slow down. And if that one machine stops, the whole site stops with it.
  2. Step 2: Put a balancer in front. A load balancer takes the public address and stands in front of a pool of identical servers. Clients talk to it as if it were the site; it passes each request to one server in the pool. The 240 requests a second become 80 for each of three servers.
  3. Step 3: Round robin. The simplest way to choose is round robin: deal requests out in turn, A, B, C, then A again. It needs no measurements and is fair when requests cost about the same, but it keeps dealing to a server even when that server is stuck on slow work.
  4. Step 4: Least connections. Least connections looks at the present load instead. The balancer counts how many requests each server is still working on and gives the next one to the smallest count: here B, with 4. Slow requests then stop piling up on one unlucky server.
  5. Step 5: Hashing. Hashing lets the request choose. The balancer turns something about it, such as the client address, into a number and takes the remainder after dividing by the number of servers. Our toy hash adds the four parts of the address: 356 leaves remainder 2, so this client always gets server C.
  6. Step 6: Health checks. A balancer must not send work to a dead server. It probes each one every few seconds, here by requesting /health. B misses three probes in a row and is taken out of the pool; A and C absorb its share, 120 each, still under their limit of 150. When B answers again it is put back.
  7. Step 7: Layer 4 or layer 7. Balancers work at one of two depths. A layer 4 balancer sees only addresses and ports and forwards the connection without reading it. A layer 7 balancer reads the HTTP request itself, decrypting HTTPS to do so, and can route by path, host name or cookie.
  8. Step 8: Sticky sessions. If a server keeps a user’s session in its own memory, her requests must keep reaching that server. A sticky session does this: the balancer sets a cookie naming the server and follows it afterwards. The price is uneven load, and a lost session if that server fails.
VALUES SO FAR
demand240 req/sinput
capacity150 req/slimit
each····result
turns····method
least····method
hash····method
B down····result
depth····kind
sticky····rule

Spotted a mistake on this page?