Scaling basics
When traffic grows, a system can scale up (a bigger machine) or scale out (more machines). Knowing when each works, and what has to change in your application before you can scale out, is the foundation of every large design.
Vertical scaling: a bigger machine
Add CPU, memory and faster disks to one server.
- ✔ Simple: no code changes, no distributed-systems problems.
- ✔ Surprisingly far-reaching: a single large server can have hundreds of cores and terabytes of RAM.
- ✖ Limits: there's a biggest machine, and prices rise steeply near the top.
- ✖ Single point of failure: when it goes down (or is upgraded), everything is down.
Scaling up is the right first move for many systems, especially databases. Don't distribute what you don't have to.
Horizontal scaling: more machines
Run many copies of the application behind a load balancer:
┌──▶ app server 1 ──┐
users ──▶ LB ───┼──▶ app server 2 ──┼──▶ database / cache
└──▶ app server 3 ──┘- ✔ Nearly unlimited capacity, on cheap commodity machines.
- ✔ Redundancy: if one server dies, the others carry on.
- ✔ Elasticity: add servers at peak, remove them at night (autoscaling).
- ✖ Needs the application to be stateless, and moves the bottleneck elsewhere (usually the database).
Statelessness
If a server keeps user data in its own memory (sessions, uploaded files, a shopping cart), the next request, sent to another server, won't find it:
To make servers stateless, move state out:
| State | Where it goes |
|---|---|
| Sessions | A shared store (Redis, a database), or a signed token/cookie the client carries |
| Uploaded files | Object storage (S3) |
| Caches | A shared cache (Redis, Memcached), or accept per-server caches that may differ |
| Scheduled jobs | A job queue or scheduler, so they don't run once per server |
Sticky sessions (the load balancer always sends a user to the same server) avoid the problem without fixing it: that server's failure still logs the user out, and load becomes uneven.
Where's the bottleneck?
Scaling is a cycle: find the bottleneck, remove it, find the next one. A request's time splits between components, and the slowest shared resource limits the whole system:
The usual order of fixes for a growing web product:
- Optimise first: add database indexes, fix N+1 queries, paginate. Often a 10× win for free.
- Scale the app tier horizontally behind a load balancer (stateless servers).
- Cache hot reads (Redis) and put static files on a CDN.
- Read replicas for read-heavy databases.
- Queues to move slow work (emails, image processing) out of requests.
- Shard the database when writes or data size outgrow one primary.
- Split services when teams and domains, not just traffic, require it.
Availability: no single points of failure
A single point of failure (SPOF) is any component whose failure takes the whole system down. Remove them with redundancy:
- At least two of everything stateless (app servers, load balancers).
- Databases with replicas and automatic failover.
- Spread across availability zones (separate data centres in one region); critical systems also across regions.
Availability is measured in "nines":
Every component in the request path multiplies in its chance of failure, which is why redundancy matters at every layer.
Autoscaling
Cloud platforms add and remove servers automatically based on metrics (CPU above 60%, requests per instance, queue length). Remember: new instances take time to start (keep a baseline), databases usually don't autoscale this way, and scale-in must drain connections gracefully.
Practice
- Change the capacity model so the database handles 8,000 queries/s. How many app servers are useful before the database becomes the bottleneck again?
- List everything in a typical web app that's stateful, and where you'd move each piece.
- A checkout flow calls five services in series, each 99.95% available. What's the availability of checkout?
1.What does "stateless" mean for an application server?
2.Adding app servers stopped increasing throughput. What's the likely reason?
3.Roughly how much downtime per year does 99.9% availability allow?