System design concepts · Think Like a Designer

How to Scale a System From One User to 100 Million

Big systems aren't designed big; they grow. Follow one app, "Snapcart", through seven stages and you'll see why every component in this book exists.

What you'll be able to explain
  • Explain why each common component gets introduced
  • Recognise the bottleneck that triggers each scaling step
  • Separate stateless and stateful tiers
  • Know which changes are cheap and which are one-way doors
  • Use this story as a backbone for any "how would you scale it?" question

Stage 1 — One box does everything

CLIENTUsersSERVICEOne serverapp + DB + filesHTTPS
Stage 1: app, database and files on one machine.

Perfect for the first thousand users. Deployments are easy, debugging is easy, and it costs very little.

Symptom that ends it: a traffic spike makes the DB and app fight for CPU and RAM; a disk failure would lose everything.

Stage 2 — Split the database out

Move the database to its own machine (or a managed service such as RDS or Cloud SQL). Now the app and the DB scale independently, and the managed DB gives you automated backups.

New problem: there is still one app server. When it dies or deploys, the site is down.

Stage 3 — Many app servers behind a load balancer

CLIENTUsersNETWORKLoad balancerSERVICEApp 1SERVICEApp 2SERVICEApp 3DATABASEDatabase
Stage 3: stateless app servers behind a load balancer.

For this to work, the app servers must be stateless: no user sessions or uploaded files on local disk. Sessions move to a shared store (Redis) or into signed tokens; files move to an object store such as S3.

Analogy

A stateless server is like a bank teller who looks up your account in the central system. Any teller can serve you. A stateful server is a teller who keeps your records in their own drawer — you must always queue for that one person.

New problem: every request still hits one database. Reads pile up.

Stage 4 — Cache and read replicas

Most apps read far more than they write. Two tools take load off the primary:

  • A cache (Redis) for hot objects: product pages, user profiles, session data.
  • Read replicas that copy the primary and serve read queries.
NETWORKLoad balancerSERVICEApp fleetCACHERedis cacheDATABASEPrimary DBwritesDATABASEReplicareadsDATABASEReplicareads1 try cachewritesreadsreplicatereplicate
Stage 4: cache in front, replicas behind.

New problem: replicas lag behind the primary by milliseconds to seconds, so a user may update their address and then see the old one. Caches can serve stale data. Welcome to consistency (Chapters Database Replication Explained and Caching and Redis for System Design Interviews).