Diseña un sistema que escale a millones de usuarios
Escala de una sola máquina a millones de usuarios para una entrevista de diseño de sistemas: balanceador de carga, réplicas, caché, CDN, colas, sharding, y lo que enseña Quorum.
You do not design a million-user system on day one. You start with one server running the app and the database, then fix whichever resource runs out first: a bigger box, a load balancer and more app servers, a cache, read replicas, a CDN, a queue for slow work, and last, sharding. Each step answers a measured bottleneck, and each one adds something to operate.
This is one of the questions in the System Design Primer, and its solution for scaling on AWS is worth reading next to this page.
Requirements#
Functional:
A user can read and write their own data.
The service grows from a handful of users to tens of millions without a rewrite.
Non-functional:
Reads outnumber writes by about 20 to 1.
Relational data, so transactions and joins matter early on.
Availability improves at each step, because one box is a single point of failure.
Estimate the load#
Assume 10 million registered users, with 20% active on a given day.
Requests: 2M daily users at 50 requests each is 100M a day, or about 1,160 a second. Peak at five times is 6,000.
Writes: 1 request in 20 writes, so about 300 writes a second at peak.
Storage: 1 KB per write at an average of 60 writes a second is about 5 GB a day. A year is under 2 TB.
One database handles a few thousand simple queries a second, so 6,000 requests with a few queries each is past one box. A year of data fits on one disk, so storage size is not what forces sharding. Write rate eventually is.
The design#
Walk it as a ladder. At each rung, name the bottleneck that sent you there.
One box#
The web server and the database share one machine, with a DNS name pointing to it. That is right for the first few thousand users. Watch CPU, memory, disk, and network so you know what runs out first.
A bigger box#
When CPU or memory runs out, buy a larger instance. It takes no code changes. The limits are cost, a ceiling on size, and a single point of failure. See vertical and horizontal scaling.
Split the app from the database, then add a load balancer#
Move the database to its own host or a managed service such as RDS, and keep static files in object storage. When the app tier saturates, run several stateless app servers behind a load balancer such as an ELB or HAProxy. The load balancer also checks health and drops dead servers.
Cache#
Most reads hit the same few rows. A cache such as Redis in front of the database turns most of them into memory lookups. See add a cache and cache-aside.
Read replicas#
When reads still outgrow the primary, add replicas. The app sends writes to the primary and reads to replicas, and replication lag means a user may not see their own write at once. See route reads and writes apart and read your writes.
CDN#
Images, scripts, and video go to a CDN such as CloudFront, which serves them near the user and takes that traffic off your servers.
Queues and workers#
Anything slow, like email, image resizing, or analytics, goes into a Queue and runs on workers. The request returns at once, and the backlog drains at its own rate.
Sharding#
When one primary cannot take the write rate, split the data across several databases by a key such as user ID. This is the costliest step, because cross-shard queries and transactions get hard. See shard the database and sharding.
Where it breaks#
Skipping the measurement. Teams add a cache when the real problem was a missing index. Profile first, fix the cheapest thing, and load test again. See find the bottleneck.
Stateful app servers. Sessions stored in server memory tie each user to one box, so the load balancer cannot spread them freely. Keep sessions in the cache or a signed cookie.
A single database after all that. Ten app servers and one primary still means one failure takes it all down. Add a standby in another zone with automatic failover.
A hot shard. A bad shard key sends most traffic to one database. Choose a key that spreads, as in hot keys and hot shards.
Build it in Quorum#
The game is built around this ladder. Start with one server that runs its own Postgres.
Drag to pan. Hold Ctrl or Cmd and scroll to zoom.
| Where | Setting | Value | Why |
|---|---|---|---|
| App Server | Local database | On | The app keeps its data on its own disk, so there is no second Node to run or pay for. |
| App Server | Instance size | micro | Micro is the starter box. It is enough for a few hundred Users. |
When the app tier is the limit, add servers behind a Load Balancer. This board shows two smaller App Servers in place of one large one, and the vertical and horizontal scaling page compares both.
Drag to pan. Hold Ctrl or Cmd and scroll to zoom.
| Where | Setting | Value | Why |
|---|---|---|---|
| App Server | Instance size | small | Two Small boxes hold the same four vCPUs as one Medium, so the comparison is fair. |
| App Server | Instance size | small | The second copy. If one dies, the other keeps serving. |
| Database | Instance size | small | Small gives the Database two vCPUs, so the figures below reflect the Cache and not a starved Database. |
Research this Design needs: Load balancing.
When the Database CPU climbs with reads, place a Cache in front.
Drag to pan. Hold Ctrl or Cmd and scroll to zoom.
| Where | Setting | Value | Why |
|---|---|---|---|
| App Server | Instance size | medium | Unchanged. |
| Cache | Engine | redis | One Redis thread answers far more operations than this traffic sends, and it reloads part of its data after a restart. |
| Cache | TTL (s) | 60 | A minute keeps most hot reads as hits and caps how old a stale answer can be. |
| Database | Instance size | small | Unchanged. It now takes only the reads the Cache misses, plus every write. |
Research this Design needs: Caching.
When reads still outgrow the primary, add a Read Replica and route reads to it. Watch for Replication lag.
Drag to pan. Hold Ctrl or Cmd and scroll to zoom.
| Where | Setting | Value | Why |
|---|---|---|---|
| App Server | Read your writes | On | A user who just saved something reads it from the primary, so a replica that has not caught up cannot show the old copy. |
| App Server | Instance size | small | The app only runs code and routes, so it stays small while the data tier grows. |
| Database | Instance size | small | The primary takes every write and the reads that must be fresh. It no longer takes the bulk of the reads. |
| Read Replica | Instance size | small | Match the primary, because it replays every write the primary takes and then answers reads on top. |
Research this Design needs: Read replicas, Read replicas, Read-your-writes routing.
When writes outgrow one primary, shard it.
Drag to pan. Hold Ctrl or Cmd and scroll to zoom.
| Where | Setting | Value | Why |
|---|---|---|---|
| App Server | Instance size | medium | Medium, so the Database is the tier that shows its limit. |
| Database | Shards | 4 | Writes split four ways, so each shard takes a quarter of them. Reads and writes now scale with the count. |
| Database | Shard key | hash | A hash of the user ID spreads Users evenly, so no shard becomes the busy one. |
| Database | Instance size | medium | Each shard is another copy of this size, so size it for one shard. |
Research this Design needs: Read replicas, Sharding.
The game also covers the other rungs without a board here. A CDN and Object Storage take static files, and a Queue with Workers takes slow jobs. The Scale the database guide orders the data steps, and Survive a traffic spike uses several at once. Research Caching, Load balancing, and Sharding to unlock each step, and run the Second Server Sandbox for the first move.
The game has no DNS records to manage, no VPC subnets, and no AWS-specific services. It does not model per-user data, so the shard key spreads simulated users and not real accounts.
Common questions#
How do you scale a web application to millions of users?#
Measure, then fix the first resource that runs out. A typical order is a bigger server, a load balancer with several app servers, a cache, read replicas, a CDN, a queue for slow work, and finally sharding.
When should you scale vertically and when horizontally?#
Scale vertically first, because it needs no code changes. Move to horizontal scaling when one machine is too expensive, too large, or a single point of failure.
When do you need to shard a database?#
When write volume or data size is more than one primary can handle, even after caching and replicas. Reads rarely force it, since replicas and caches scale reads more cheaply.
Does the System Design Primer require AWS for this question?#
No. The Primer solves it on AWS, but it says the principles apply anywhere. The ladder of load balancer, cache, replicas, CDN, queues, and sharding is the same on any cloud.