Loading…
Loading…
Charting your lesson…
Opening your lesson…
Sharding & Replication
Lesson 4 of 6
Splitting data across nodes (sharding) for scale and copying it (replication) for availability and read throughput.
6 steps•about 10 min•8 flashcards
Step 1/ 61 min
Once a single database can't keep up, two techniques carry the load: replication (copy the data) and sharding (split the data). Interviewers use these to test whether you can grow a database beyond one machine — and whether you understand the costs.
Step 2/ 61 min
Replication keeps multiple copies of the same data on different nodes. It boosts read throughput and availability — if one copy dies, another serves. Sharding (partitioning) splits one large dataset into disjoint pieces (shards) spread across nodes, so each holds only a fraction. Sharding tackles write scaling and storage; replication tackles reads and fault tolerance. Big systems use both together.
Replication can be synchronous (wait for replicas to confirm — safer, slower) or asynchronous (don't wait — faster, but replicas lag, causing possible stale reads).
Sharding introduces cross-shard JOINs and transactions, which are slow or need distributed protocols. A bad shard key creates hotspots or celebrity-key problems. Rebalancing when adding nodes is disruptive (which is why consistent hashing helps). Replication introduces replication lag and, with async, the choice between reading from the primary (fresh) or a replica (fast but possibly stale). With multi-primary you must resolve write conflicts.
Step 3/ 61 min
A messaging app's single DB can't handle write volume. You shard by user_id (hash-based) across four nodes so writes spread evenly, and give each shard two async read replicas for read scaling and failover. A user's messages live on one shard; that shard's primary takes writes while replicas serve history reads. If a primary fails, a replica is promoted.
Step 4/ 61 min
Add replication first — it's simpler and covers reads plus availability. Add sharding only when a single node truly can't hold the data or absorb the write rate, since it adds real operational complexity. Choose the shard key by your dominant access pattern.
Step 5/ 61 min
Remember "Replicate for reads and resilience; shard for size and writes." Sharding = splitting (disjoint pieces); replication = copying (same data many times).
Don't confuse the two. Replication does NOT increase total storage capacity or write throughput on the primary — every copy holds the full dataset and every write still goes through the primary. Only sharding splits writes and storage.
Step 6/ 6· Worked examples1 min
Problem
A users table has grown to 500 million rows — too big and too busy for one database node. Design a scheme that scales storage/writes AND survives a node failure.
Try it on paper first.
Recall
Keep this
Fast revision of scaling a database beyond one machine.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.