Sharding and replication, step by step
One database can only grow so far. Watch an app outgrow it: first too many reads, then too many writes and too much data, and see replicas, shards and shard keys appear for a reason.
3 steps · about 6 minutes · free, no sign-up
Step 1: Reads pile up
Profiles, feeds and searches: reads outnumber writes 20 to 1.
The database is overloaded, and 95% of the queries are reads. What do you do?
- Add more app servers (Not this one): The app servers were waiting on the database. More of them just queue up more queries for it.
- Add read replicas (Right fix): Writes still go to the primary, which streams every change to a replica. Reads are spread over the replica, so the primary only does writes. Replication: Keeping copies of a database on other machines. Replicas share the reads and can take over if the primary fails. They are a moment behind, so read fresh writes from the primary.
- Buy a bigger database server (Helps for a while): Quickest fix, and it works for a while. But the biggest servers are very expensive, and it is still one machine doing every read.
Step 2: Too many writes
5 million users, 4 TB of data, and the primary can’t keep up with writes.
Replicas took the reads, but every write still goes to one primary, and the data no longer fits comfortably on one machine. What do you do?
- Add more read replicas (Not this one): Replicas only help reads. Every write still lands on the same primary, and each new replica is one more copy it has to feed.
- Split the users across two databases (shards) (Right fix): Users 1 to 5 million live on shard 1, the rest on shard 2. Each shard is a full database with its own replica, taking half the writes and half the data. Sharding: Splitting one big dataset across several databases, each holding a slice. The app routes each query to the right shard by a shard key (here, the user ID). It is how big apps scale writes.
- Buy the biggest database server there is (Helps for a while): It buys months, not years. Writes and data keep growing, and you have nowhere left to go when it fills up.
Step 3: One hot shard
Every new user gets the next ID, so all sign-ups and their activity land on shard 2.
Shards split by ID ranges: the newest, most active users all land on the last shard while the first one sits idle. What do you do?
- Give the hot shard a bigger machine (Helps for a while): It copes for now, but you have only moved the problem: the newest shard is always the hot one, and the old ones stay idle.
- Pick the shard by a hash of the user ID (Right fix): A hash scrambles IDs, so new and old users are spread evenly across both shards. Each one gets about half of everything. Shard key: The value that decides which shard a row lives on. A good key spreads data and traffic evenly (a hash of the user ID); a bad one creates hot spots (sign-up date, country).
- Put new users in a cache instead (Not this one): A cache can speed up reads, but users’ data must be stored in a database. Writes still pile onto shard 2.
What you built
- Primary: The one copy that accepts writes for its data.
- Read replica: A read-only copy that shares the reads. Can lag slightly behind.
- Sharding: Splitting data across several databases so writes and storage scale out.
- Shard key: Decides where each row lives. Hash it so data and traffic spread evenly.
| Problem | Fix | Trade-off |
|---|---|---|
| Too many reads | Read replicas | Replicas lag a little |
| Too many writes or too much data | Sharding | Queries across shards get harder |
| One shard is much busier | Hash the shard key | Range queries touch every shard |
- Lesson: caching strategies
- Lesson: from 1 user to 1 million
- Read: how to scale a web app to 1 million users
Frequently asked questions
What is the difference between replication and sharding?
Replication copies the same data to several machines, which helps reads and failover. Sharding splits different data across machines, which helps writes and storage. Big systems use both.
When should I shard a database?
When a single primary can’t handle the writes or hold the data, even after replicas, caching and a bigger machine. Sharding adds complexity, so do it late.
What makes a good shard key?
One that spreads data and traffic evenly and matches how you query, like a hash of the user ID. Avoid keys where new activity clusters, such as dates or a growing ID range.
What is replication lag?
The short delay before a change on the primary appears on a replica. Read your own fresh writes from the primary to avoid showing old data.