Lessons › From one user to millions
Split the database
Your app went write-heavy: 20,000 req/s, and 40% are writes. That's 8,000 writes/sec, and the biggest primary handles 4,800.
Play this levelFree, no account needed. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Replicas can't help here: every copy still has to take every write. A bigger machine stops helping once you hit the largest size. This is the wall that sharding exists for.
Sharding splits the data across several primaries, for example by user ID. Each one holds part of the data and takes only that part's writes.
It isn't free. Each shard costs a full database, a query across shards gets slow, and one dead shard still fails its share of users. Shard only when nothing else works.
What you have to achieve
20,000 requests a second at the busiest hour, 40% of them writes.
- No broken or illegal connections
- No component is overloaded
- Fewer than 0.1% of requests fail
- p99 response time under 300 ms
- Machines cost at most $3,700 / month
What you learn
You scaled writes past a single machine. Notice the bill: sharding is often the most expensive line in the design, which is why teams put it off.
Check yourself
Why couldn't read replicas fix this level?
- They were too expensive
- They're too slow
- Every replica still has to take every write
Show the answer
Every replica still has to take every write. Replicas copy every write, so they add read capacity, not write capacity. Writes past one machine need sharding.
What does sharding do?
- Splits the data across several primaries, each taking its own part's writes
- Copies all data to every machine
- Moves data into a cache
Show the answer
Splits the data across several primaries, each taking its own part's writes. Each shard holds part of the data, for example by user ID, and takes only that part's writes.