Load balancers
A load balancer sits in front of a group of servers and spreads incoming requests across them. It's what makes horizontal scaling work: users talk to one address, and behind it you can run 3 servers or 300, add and remove them, and survive individual failures.
┌─▶ server A ✔
clients ──▶ load ────┼─▶ server B ✔
balancer ├─▶ server C ✖ failed health check: no traffic
└─▶ server D ✔What a load balancer does
- Distributes requests by an algorithm.
- Health-checks servers (
GET /healthzevery few seconds) and stops sending traffic to failed ones. - Terminates TLS, so app servers don't each manage certificates.
- Drains connections when a server is being removed, for zero-downtime deploys.
- Often also: rate limiting, compression, request routing by path or host, and protection against some attacks.
Layer 4 vs layer 7
| Layer 4 (transport) | Layer 7 (application) | |
|---|---|---|
| Sees | IP addresses and ports (TCP/UDP) | Full HTTP: URL, headers, cookies |
| Can route by | Connection | Path (/api → API servers, /images → image servers), host, header |
| Speed | Very fast, simple | More work per request, far more flexible |
| Examples | AWS NLB, HAProxy (TCP mode), LVS | AWS ALB, nginx, Envoy, HAProxy (HTTP), Traefik, Cloudflare |
Balancing algorithms
When requests aren't equal: least connections
Round robin assumes every request costs the same. When some take 10 ms and others 2 seconds, a server can be swamped with slow requests while another sits idle. Least connections sends each request to the server with the fewest requests in progress:
Other smart strategies: least response time, and power of two choices (pick two servers at random and use the less busy one), which gets most of the benefit with very little coordination.
Health checks and failover
Two kinds of checks matter: liveness ("is the process alive?") and readiness ("can it serve traffic right now?", e.g. database connected, caches warmed). A server that's starting up or shutting down is alive but not ready.
The load balancer is a single point of failure, too
Load balancers are deployed in redundant pairs or clusters, often sharing a virtual IP that moves to the standby if the active one fails, or (in the cloud) as a managed service that's redundant across availability zones. At the very top, DNS spreads users across load balancers in different regions (GeoDNS, anycast).
Sessions and stickiness
Sticky sessions route a user to the same server every time (via a cookie or IP hash). They're sometimes needed for legacy apps or WebSocket connections, but they unbalance load and lose sessions when a server fails. Prefer stateless servers with sessions in a shared store, as in the scaling lesson.
Global load balancing
For worldwide products:
- GeoDNS answers each user with the nearest region's address.
- Anycast announces the same IP from many locations; the internet routes users to the closest one (how Cloudflare and Google's front ends work).
- Each region has its own load balancers; if a region fails, DNS or anycast steers traffic to the next nearest.
Practice
- Add "least response time" to the simulation, tracking each server's average duration.
- Change the health check to mark a server healthy only after 2 consecutive successes. Why might that be wise?
- Design path-based routing:
/api/*to API servers,/static/*to a CDN origin, everything else to web servers.
1.What does a layer 7 load balancer understand that a layer 4 one doesn't?
2.Why is least connections better than round robin when request durations vary a lot?
3.What happens when a server fails its health checks?