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.

Beginner 4 min readLesson 2 of 20

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

Vertical (scale up)

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
Horizontal (scale out)

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:

StateWhere it goes
SessionsA shared store (Redis) or a signed token (JWT) carried by the client
Uploaded filesObject storage (S3), not local disk
CachesA shared cache tier, or accept per-instance caches that may differ
Background jobsA queue, not in-memory timers

The scaling journey

A typical path from a side project to a large service:

  1. One server runs the web app and database together.
  2. Separate the database onto its own machine; scale each independently.
  3. Add a load balancer and several app servers (now they must be stateless).
  4. Add a cache (Redis) for hot reads, and a CDN for static assets.
  5. Database read replicas to scale reads; send writes to the primary.
  6. Queues and workers to move slow work (emails, image processing) off the request path.
  7. Shard the database when writes or data size outgrow one primary.
  8. Split into services owned by different teams, when the organisation and domain demand it.
  9. 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.

SymptomLikely bottleneckTypical fix
App CPU high everywhereApplication computeMore instances, optimise hot code
DB CPU high, slow queriesDatabaseIndexes, query fixes, caching, replicas
DB connections exhaustedConnection limitsConnection pooling (PgBouncer), fewer instances per DB
High latency, low CPUWaiting on I/O or downstream servicesAsync I/O, timeouts, parallel calls, caching
Memory pressure / GC pausesHeap size, leaksTune, fix leaks, scale out
One shard or key overloadedHot spotBetter 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.

Key takeaways
  • 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.