Replication & Sharding
Before this, it helps to know: SQL vs NoSQL, Numbers Every Beginner Should Know.
Read a little, play a little. No scary maths, and no rush.
One database hits a wall
Your app takes off. Reads climb 10×, then the write load follows. A single database — no matter how beefy — eventually runs out of CPU, disk, or network. You have two moves, and they solve different halves of the problem.
Move 1 — Replication: clone for reads
Keep one leader that takes all writes, and stream copies to two or three replicas that serve reads. Product pages, search results, dashboards — all read from replicas, spreading the load. The catch is lag: replicas trail the leader by milliseconds, so a user might update their name and briefly see the old one. For most reads, that staleness is invisible and totally acceptable.
Beginner setup to name in interviews: one leader, two replicas, plus a cache. It handles the vast majority of read-heavy apps.
Move 2 — Sharding: split for writes
Replicas don't help writes — every write still lands on the leader. So shard: split rows across databases by something like hash(user_id) % 4. User 1–25M live on shard A, 25–50M on shard B, and writes spread across all four. Reads route by the same hash, so every query knows exactly which shard to ask.
The price: queries spanning shards ("total revenue across all users") get painful, rebalancing when you add shard #5 is real work (this is where consistent hashing earns its name), and transactions can't easily span shards.
The celebrity problem
Imagine a celebrity with 50 million followers posts at once — every one of those reads hammers the same row on the same shard. That's a hot key, and it melts one machine while the others idle. Fixes: cache the row aggressively, split counters into sub-keys summed at read time, and add jitter so millions of clients don't stampede the same second.
Reading your own writes
Replica lag causes one especially confusing moment: a user saves their profile, the page reloads from a replica that has not caught up, and their change seems to have vanished. A common fix is to send a user's reads to the leader for a few seconds after they write, or to wait until the replica has caught up to that write. Everyone else keeps reading from replicas.
When the leader dies
Failover promotes a replica to be the new leader. If replication was asynchronous (the leader confirmed writes before replicas received them), the last few writes may be lost in the switch. Synchronous replication waits for at least one replica to confirm each write, which closes that gap at the cost of slower writes. Many systems use a mix: one synchronous replica for safety, the rest asynchronous for speed.
Choosing a shard key
A good shard key spreads data and traffic evenly and keeps most queries inside a single shard. user_id is often a good choice for user-centric apps. Poor choices include:
- Country: a few countries hold most users, so a few shards carry most of the load.
- Creation time: every new write lands on the newest shard, which becomes a hot spot.
Changing a shard key later means moving data between shards, so it is one of the few design choices worth getting right early.
Remember this
- Reads → replicate. Writes → shard. Hot keys → cache + split.
- Say the lag tradeoff aloud: replicas buy scale with milliseconds of staleness.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
Moving a checkout script to Redis Cluster
Senior · 2 questions
GitLab deletes its production database
Intermediate · 2 questions
Serverless functions run out of database connections
Intermediate · 2 questions
Long reports keep getting cancelled on the replica
Senior · 2 questions
An old name comes back for a day
Senior · 2 questions
Discord's hot partitions and trillions of messages
Senior · 2 questions
Related chapters
Spotted a mistake or want a topic covered? Report an issue