Scalability Basics
Vertical vs horizontal scaling, stateless services, the evolution from one server to millions of users, and the bottlenecks you'll hit along the way.
A system is scalable if it can handle growing load — more users, more data, more requests — by adding resources, without being redesigned and without performance collapsing.
Vertical vs horizontal scaling
Buy a bigger machine: more CPU, RAM, faster disks.
- Simple: no code changes
- Hard upper limit, and cost rises steeply at the top end
- The single machine is a single point of failure
- Today's servers are huge (hundreds of cores, terabytes of RAM), so this goes far
Add more machines and spread the load.
- Near-unlimited capacity; commodity hardware
- Fault tolerance: one machine failing isn't fatal
- Requires distribution: load balancing, stateless design, data partitioning
- Brings distributed-systems problems (consistency, partial failure)
Stateless services
The key to scaling application servers horizontally: don't keep per-user state in a server's memory. If any request can go to any server, you can add or remove servers freely.
Move state out of the app servers:
| State | Where it goes |
|---|---|
| Sessions | A shared store (Redis) or a signed token (JWT) carried by the client |
| Uploaded files | Object storage (S3), not local disk |
| Caches | A shared cache tier, or accept per-instance caches that may differ |
| Background jobs | A queue, not in-memory timers |
The scaling journey
A typical path from a side project to a large service:
- One server runs the web app and database together.
- Separate the database onto its own machine; scale each independently.
- Add a load balancer and several app servers (now they must be stateless).
- Add a cache (Redis) for hot reads, and a CDN for static assets.
- Database read replicas to scale reads; send writes to the primary.
- Queues and workers to move slow work (emails, image processing) off the request path.
- Shard the database when writes or data size outgrow one primary.
- Split into services owned by different teams, when the organisation and domain demand it.
- Multiple regions for global latency and disaster recovery.
Each step adds complexity, so take it only when measurements say you need it.
Finding the bottleneck
Load is limited by the most constrained resource — adding capacity anywhere else doesn't help.
| Symptom | Likely bottleneck | Typical fix |
|---|---|---|
| App CPU high everywhere | Application compute | More instances, optimise hot code |
| DB CPU high, slow queries | Database | Indexes, query fixes, caching, replicas |
| DB connections exhausted | Connection limits | Connection pooling (PgBouncer), fewer instances per DB |
| High latency, low CPU | Waiting on I/O or downstream services | Async I/O, timeouts, parallel calls, caching |
| Memory pressure / GC pauses | Heap size, leaks | Tune, fix leaks, scale out |
| One shard or key overloaded | Hot spot | Better key, split hot keys, cache |
Autoscaling
Cloud platforms add and remove instances automatically based on metrics (CPU, request rate, queue depth):
- Scale on a metric that tracks load well (requests per instance or queue length is often better than CPU).
- New instances need time to start and warm up, so keep headroom and scale out early.
- Scale-in should be slower and graceful (drain connections).
- Downstream systems (databases) usually don't autoscale — protect them with pooling, rate limits and caching.
Performance vs scalability
- A performance problem: the system is slow for a single user.
- A scalability problem: it's fast for one user but slow under heavy load.
Fix performance first; scaling out a slow design multiplies the cost.
- Vertical scaling is simple but limited; horizontal scaling is unlimited but needs distribution.
- Stateless app servers (sessions and files moved out) make horizontal scaling easy.
- Grow step by step: separate DB → load balancer → cache/CDN → replicas → queues → sharding → services → regions.
- Always find the actual bottleneck before adding capacity.
- Autoscale stateless tiers; protect stateful dependencies.