Scaling from zero to millions of users
Most systems grow through the same few steps, from one server to many parts. Learn the usual order, what makes each step worth it, and why you should measure before you add anything.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This story is made up, but it is common. A small team builds a ticket booking app. It runs on one server: the web app and the database together. For a year this works fine.
Then the team plans for growth. In one quarter they add Kafka, a Redis cluster, six microservices and a sharded database. They have 3,000 daily users.
A popular concert goes on sale. The site slows to a crawl. The team looks at the dashboards they have, and none of them show the problem. After two days of searching, they find it. One slow SQL query with no index was reading the whole bookings table on every page load.
The new parts did not help. They made the system harder to debug, and nobody had measured anything before adding them. One index fixed the problem.
Scaling is not a list of tools to install. It is a series of answers to problems you can see in your numbers.
The idea in simple words
A well-known chapter of ByteByteGo's System Design Interview course walks one system from a single user to millions, one step at a time. 1 The steps are not a law. Treat them as a typical order: in our experience, each step fixes the most likely next bottleneck.
The rule behind every step: find what is full, then fix only that. The chapter ends by saying that scaling a system is an iterative process. 1
The key idea. Each part you add fixes one kind of limit and adds new ways to fail. Add a part when a measurement says you need it, not because big companies have it.
How it works
Step through how the system grows. Use Next and Back, or the ← and → keys.
- Day one. The app and the database run on one server. This is cheap and simple, and it is often enough for a long time.
- The database moves to its own machine. Now the app and the database can each get the size they need.
- A load balancer sends users to several app servers. The app servers are stateless, so any of them can serve any user, and one can fail without taking the site down.
- Reads grow. A cache answers the most common reads, and a read replica takes reads off the main database.
- A CDN serves images and scripts from places near users. A queue takes slow work, such as resizing photos, so workers do it in the background.
- When one database can no longer take all the writes, the data is split into shards. Monitoring of latency, traffic, errors and saturation was needed from day one. At this size you cannot run without it.
0. Measure from day one
Before any step, you need numbers. The Google SRE book names four golden signals: latency, traffic, errors and saturation. If you can measure only four metrics of a user-facing system, measure these. 3 Saturation tells you what is full. Latency and errors tell you if users feel it. Use back-of-envelope estimates to guess what will fill up next.
1. One server
At the start, everything runs on one server. 1 It is cheap and easy to debug. Its weakness: if that server goes offline, users cannot reach the site. 1
2. A separate database
Move the database to its own machine. 1 Now the app and the database stop fighting for the same CPU and memory, and you can size each one on its own.
Trigger (our advice): the app and the database compete for CPU, memory or disk on one machine.
3. Bigger machine, or more machines?
Vertical scaling means a bigger machine: more CPU and RAM. Horizontal scaling means more machines. When traffic is low, vertical scaling is a good choice because it is simple. 1 But it has a hard limit, and one machine gives you no failover. 1
AWS's reliability advice is to replace one large resource with several small ones, so one failure hurts less. 2
Trigger (our advice): scale up first while it is cheap. Scale out when you near the largest machine, or when one machine going down is not acceptable.
4. A load balancer and stateless app servers
A load balancer spreads requests across several app servers. 1 For this to work, the app servers must be stateless. Move session data out of server memory into a shared store, so any server can handle any request. 1 See Load balancing for how the balancer picks a server.
Trigger: one app server is near its CPU limit, or you need to survive one server dying.
5. Read replicas
Many apps read far more than they write. With replication, the main database takes the writes and replicas serve the reads. 1 Replicas can lag behind the main database, so a user may not see their own write at once. See Replication.
Trigger: database CPU is high and most queries are reads. Check first that slow queries are not the real cause.
6. A cache
A cache keeps often-read data in memory, so fewer reads reach the database. 1 It adds new problems: stale data and many requests missing at once. See Caching and cache stampedes.
Trigger: the same data is read again and again, and it can be a little out of date.
7. A CDN for static files
A CDN is a network of servers spread around the world that serves static content, such as images, CSS and JavaScript. 1 Users get files from a nearby server, and your servers do less work. See CDNs and HTTP caching.
Trigger: static files use much of your bandwidth, or users far away see slow page loads.
8. Queues for slow work
Some work does not need to finish before you reply to the user. In the chapter's example, web servers put photo processing jobs on a message queue, and workers do them in the background. 1 The queue also lets the two sides scale and fail on their own. See Queues vs logs.
Trigger: a request waits on slow work, such as sending email or resizing images, that the user does not need to see at once.
9. Sharding
Replicas help with reads. They do not help when one database cannot take all the writes or hold all the data. Then you split the data across shards, each with a part of the rows. 1 Sharding is hard to undo, and queries across shards get harder. See Sharding and consistent hashing.
Figma is a real example of this order. In 2020 it ran one Postgres database on AWS's largest physical instance. 4 By the end of 2022 it had added caching, read replicas and a dozen databases split by table. 4 It moved to horizontal sharding only when some tables reached several terabytes and billions of rows. Their writes would also soon pass the IOPS limit of Amazon RDS. Its first sharded table shipped in September 2023. 4
Trigger: writes or data size on one database near its limits, after indexes, query fixes, replicas and splitting tables are done.
After that
The chapter goes further, to several data centers. 1 See Multi-region systems when you get there.
Trade-offs
| Step | What it fixes | What it costs |
|---|---|---|
| Bigger machine | More CPU and memory, with no code change. | A hard size limit. Still one point of failure. |
| Separate database | The app and database stop fighting for resources. | One more machine and one network hop. |
| Load balancer and stateless servers | More traffic. One server can die safely. | Sessions must move to a shared store. |
| Read replicas | Read load on the database. | Replica lag. Users may read old data. |
| Cache | Repeated reads of hot data. | Stale data, stampedes, one more system to run. |
| CDN | Static file load and distance to users. | Purging old files. Cost per GB. |
| Queue | Slow work blocking requests. | Duplicates, retries, a backlog to watch. |
| Sharding | Write load and data size on one database. | Cross-shard queries, resharding, hot keys. Hard to undo. |
Simple rule (our advice): go down this table only as far as your numbers force you to.
At work: how a senior uses this
- Ask "What is full?" before any scaling proposal. Ask for the graph that shows it.
- Fix the cheap things first: a missing index, an N+1 query, a bigger instance.
- Write the trigger down. For example: "We add a read replica when database CPU stays above 70% at peak." Then the decision is not a debate later.
- Make app servers stateless early. It is cheap when the code is small and painful later.
- For each new part, ask who runs it, how it fails and how you will see it fail.
- Use the AWS principle "stop guessing capacity": watch demand and add or remove resources to match it. 2 Remember that cloud quotas still apply. 2
- Plan for load tests and capacity planning before big events, not after.
With AI
What to ask the agent
"Here are our dashboards for latency, traffic, errors and CPU, memory and disk for each machine. Which resource is closest to full at peak? What is the cheapest change that buys us 3x headroom?"
"Find any place where this app keeps user state in server memory, such as sessions, uploads on local disk or in-process caches. List what must move before we run more than one server."
"List the 10 slowest database queries from this log, with how often each runs. Suggest an index or query change for each, before we talk about replicas."
What to check yourself
- The agent's plan starts from a measured bottleneck, not from a standard diagram.
- It did not suggest sharding or microservices when an index or a bigger machine would do.
- Any proposed cache says how data is kept fresh and what happens on a miss.
- Each new part comes with the metric that shows it works and an alert for when it fails.
- Cost is in the plan. More parts mean more servers to pay for and more on-call work.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 4 sources and related lessons
- ByteByteGo, “Scale From Zero To Millions Of Users”, System Design Interview course (online chapter) (opens in a new tab)
The page names no author. A step-by-step walk from one server to load balancers, replicas, cache, CDN, stateless servers, data centers, queues and sharding. - AWS Well-Architected Framework, Reliability Pillar, “Design principles” (opens in a new tab)
Scale horizontally to limit the impact of one failure, and stop guessing capacity by watching demand. - Rob Ewaschuk, “Monitoring Distributed Systems”, Site Reliability Engineering (Google, O'Reilly 2016), chapter 6 (opens in a new tab)
The four golden signals to measure first for any user-facing system. - Sammy Steele, “How Figma's databases team lived to tell the scale”, Figma blog (March 2024) (opens in a new tab)
A real path from one large Postgres database to caching, read replicas, split databases and then sharding.
Case studies: Figma: Horizontally sharding Postgres behind a query proxy · Zerodha: Scaling a large broker with simple, boring choices
Related: Latency numbers and back-of-envelope maths · Load balancing · Replication · Caching and cache stampedes · CDNs and HTTP caching · Queues vs logs and delivery guarantees · Sharding and consistent hashing · Observability, SLOs and error budgets · Capacity planning (coming soon) · Multi-region systems and data locality
Every step after the first one turns your app into a distributed system. Read Why distributed systems are hard next. For reads and writes in depth, see Scaling reads and Scaling writes and hot keys.