SproutStackSproutStack home

Replication & Sharding

6 min readBeginner-friendlyFree to read

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.

Share:

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.

writesreadsreplicatesAppLeadertakes every write2 replicasserve reads
Writes go to the leader and reads spread across replicas. Replicas trail the leader by milliseconds: the price of read scale.

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.

Apphash(user_id) % 4Shard Ahash % 4 = 0Shard Bhash % 4 = 1Shard Chash % 4 = 2Shard Dhash % 4 = 3
Each user lives on exactly one shard, so writes spread across four databases and every query knows which one 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

1. What problem does read replication primarily solve, and what does it cost?
2. Why do replicas not solve a write-heavy scaling problem, and what does sharding add?
3. What is a hot key problem, and which fixes does the chapter name?
4. What does consistent hashing help with in a sharded database?
5. Why is a single global counter a bottleneck when generating IDs from a database?

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

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.