Load/Bearing
Fundamentals · 02 of 22

Load balancing and horizontal scaling

Add machines instead of buying bigger ones, and put something in front that spreads the work and routes around failures.

3 min readUsed in: 2 · Front Page, 11 · Four Nines

When one server can't keep up, you have two options. Vertical scaling means a bigger machine: more cores, more memory. It's simple and needs no code changes, but it has a ceiling, gets expensive fast, and leaves you with one machine that can take everything down.

Horizontal scaling means more machines of the same size. It has no hard ceiling and gives you redundancy for free, but it needs two things: application servers that keep no local state, and a load balancer to spread requests between them.

USERSclientsLBhealth checksAPP 1statelessAPP 2statelessAPP 3statelessDBshared state
A load balancer spreads requests across identical, stateless app servers. Shared state lives in the database.

Stateless first

A server is stateless when it keeps nothing between requests that another server would need. Sessions go in a shared store (Redis, the database, or a signed cookie), and uploads go to object storage. If any server can answer any request, adding capacity is as simple as starting another copy.

The alternative, sticky sessions, pins each user to one server. It works, but load becomes uneven, and when that server dies its users lose their sessions.

Layer 4 vs layer 7

Layer 4 (transport)Layer 7 (application)
SeesIP addresses and portsHTTP method, path, headers, cookies
Can route byConnectionURL, host, header, user
CostVery cheap, very fastTerminates TLS and parses HTTP
ExamplesAWS NLB, IPVSNGINX, Envoy, HAProxy, AWS ALB

Most web systems use an L7 balancer because it can send /api and /static to different pools, retry idempotent requests, and terminate TLS in one place.

Choosing a server

Health checks

The load balancer probes each server, for example with GET /healthz every few seconds, and stops sending traffic after a few failures in a row. This is what turns a crashed server from an outage into a small loss of capacity. Make the check meaningful but cheap. If it calls the database, a slow database can mark every server unhealthy at once.

Why 95% busy is too busy

Requests don't arrive evenly. When a server is busy, new requests wait in line. Basic queueing theory (an M/M/1 queue) gives the average time in the system as:

response time ≈ service time ÷ (1 − utilisation)
UtilisationResponse time vs idle
50%2×
75%4×
80%5×
90%10×
95%20×

A server at 95% is technically keeping up, but every request takes 20 times longer than it would on an idle machine. That's why capacity plans aim for 60–75% at peak. A related rule, Little's law (L = λW), says requests in flight equal arrival rate times time in the system. As latency climbs, so do open connections and memory use.

Don't make the load balancer the new single point of failure

Run load balancers in pairs with a floating IP, or use a managed service that is redundant across zones. At global scale, DNS or anycast spreads users across load balancers in several regions.

Autoscaling

Horizontal scaling also lets you add and remove servers automatically, based on CPU, request rate or queue depth. Scale out early and scale in slowly: a new instance takes time to boot and warm up, and scaling in too eagerly makes capacity flap up and down.

In the game: without a load balancer, only APP 1 gets traffic, however many servers you add. Each app server's latency follows the 1 ÷ (1 − ρ) curve. In Front Page, two servers run at 75% at peak and respond four times slower than idle; three run at 50%. In Four Nines, the health check is what saves you when APP 1 crashes.

Interview checklist

Practise it

Level 2: Front Page

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 2

Further reading