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.
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) | |
|---|---|---|
| Sees | IP addresses and ports | HTTP method, path, headers, cookies |
| Can route by | Connection | URL, host, header, user |
| Cost | Very cheap, very fast | Terminates TLS and parses HTTP |
| Examples | AWS NLB, IPVS | NGINX, 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
- Round robin: each server in turn. Fine when requests cost about the same.
- Weighted round robin: bigger servers get a bigger share.
- Least connections: pick the server with the fewest requests in flight. Better when request cost varies a lot.
- Power of two choices: pick two servers at random and send to the less busy one. Nearly as good as least connections and much cheaper to run at scale.
- Hashing (IP or key): the same client always lands on the same server. Useful for caches, but it rebalances badly unless you use consistent hashing.
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:
| Utilisation | Response 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
- Say the app tier is stateless and where sessions live.
- Pick L4 or L7 and give a reason.
- Name the balancing algorithm and the health check.
- Size for 60–75% utilisation at peak, plus one spare server (N+1).
- Make the load balancer itself redundant.
Level 2: Front Page
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 2Further reading
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.