Exercises — Distributed databases — sharding strategies, replication
Everything here builds on the parent note. If a term feels shaky, the prerequisite is linked the first time it appears.
L1 — Recognition
You should be able to name the right concept without any calculation.
Exercise L1.1
A table of 50 TB cannot fit on one server. You cut it into disjoint pieces, each row living on exactly one node. What is this called, and what problem does it solve?
Recall Solution
This is sharding (also called horizontal partitioning — see Horizontal vs Vertical Partitioning). Problem solved: scale — one machine cannot hold the whole dataset or serve all the traffic. We split distinct data across many nodes. It is not replication: replication would copy the same data, not split it.
Exercise L1.2
For each phrase, say whether it describes sharding or replication: (a) "keep three copies of every row so a crash loses nothing" (b) "users A–H on node 0, I–P on node 1" (c) "serve reads from many machines to spread read load" (d) "each row lives on exactly one primary node chosen by a key"
Recall Solution
(a) Replication — copies of the same data (durability). (b) Sharding — distinct rows split by range. (c) Replication — read scaling comes from having copies to read from. (d) Sharding — a shard key routes each row to one owner.
Memory anchor: Sharding = split (scale). Replication = copy (safety).
Exercise L1.3
In the quorum rule , name what each of , , stands for.
Recall Solution
- = total number of replicas (copies) of the data.
- = number of replicas a write must reach before it is acknowledged.
- = number of replicas a read must contact. See Quorum Consensus and Paxos/Raft for the machinery behind it.
L2 — Application
Now plug numbers into the formulas.
Exercise L2.1
You run shards with shard = hash(key) mod N holding 2,100,000 keys. You add a shard (now ). About how many keys must migrate?
Recall Solution
Under naive mod, the moved fraction is Keys moved . Why so many? Changing the modulus from 7 to 8 changes the residue class of almost every key, so almost every key gets a new home.
Exercise L2.2
Same starting point (, 2,100,000 keys, add one node) but you use consistent hashing with evenly placed virtual nodes. About how many keys move now?
Recall Solution
Keys moved . Why so few? The new node steals exactly one arc of the ring — the arc between it and its clockwise neighbour. Everyone else keeps their keys.
Exercise L2.3
replicas. You want strong consistency (read always sees the latest write) while tolerating 1 replica down for writes. Give a valid and check it.
Recall Solution
Tolerating 1 node down for writes means we can only require . Strong consistency needs : Smallest read set: . Check: ✓. So works. This also tolerates up to replicas down for reads.
L3 — Analysis
Reason about why a configuration behaves as it does.
Exercise L3.1
. A team picks "for speed." Show precisely when a read can return stale data, and fix it with the minimum change.
Recall Solution
Check the rule: . Fails. Why staleness happens (pigeonhole): the write lands on some single replica (size 1), the read hits some single replica (size 1). Overlap is forced only when ; here , so the two singletons can be completely different replicas — the reader may hit a copy that never saw the write. Minimum fix: bump one of them so the sum exceeds 3. E.g. : — still fails! You need , so : ✓. Overlap replica guaranteed.
Exercise L3.2
For , someone chooses . It satisfies . Explain the hidden cost and when writes will simply fail.
Recall Solution
✓ — reads are super fast (any 1 replica is enough, since a write touched all 3). Hidden cost: means a write must reach every replica. Write fault tolerance is . If even one replica is down or slow, the write cannot complete. So this trades write availability for cheap reads — good only for read-heavy, rarely-written data.
Exercise L3.3
A time-ordered key (created_at) is used as the shard key with range sharding across 4 shards. Predict the load pattern and explain the mechanism; then say which single change removes the hotspot without losing all range-query ability.
Recall Solution
Prediction: a hotspot — every new write lands on the last range (the newest timestamps), so one shard absorbs ~100% of write traffic while three sit idle.
Mechanism: ranges are contiguous and time is monotonically increasing, so the "current" time always falls in the highest range. See the two panels in the figure below.
Fix: use a compound / hashed prefix shard key (e.g. hash(user_id) as the leading component, created_at secondary). This spreads writes across shards (Load Balancing) while still letting you range-scan within a user. Pure hash(created_at) would kill all range queries, so we keep time as a secondary sort key.

L4 — Synthesis
Combine sharding and replication in one design.
Exercise L4.1
You shard 12,000,000 rows across shards using consistent hashing, and replicate each shard times. (a) How many physical data copies exist in total? (b) How much data (rows) does one physical node hold, assuming even spread and one shard-replica per node?
Recall Solution
(a) Each of the 6 shards is stored 3 times: physical shard-replicas (copies). (b) Rows per shard . Each physical node holds one shard-replica rows. Reading it: sharding cut the 12M rows into 6 disjoint 2M pieces (scale); replication then made 3 copies of each piece (safety). The two axes multiply: boxes.
Exercise L4.2
On the same system ( replicas per shard), you want a read to see your own latest write with the cheapest reads possible, while still surviving 1 replica failure during writes. Give per shard and justify each number.
Recall Solution
- Survive 1 replica down on write ⇒ .
- Cheapest reads that still guarantee overlap: , so . Wait — can we get ? That needs , which loses write fault tolerance. So with the "survive 1 write failure" constraint, the cheapest read is . Answer: per shard. This is the classic strong-consistency default. (See Replication Lag and Read-Your-Writes for why must overlap the write set to beat lag.)
Exercise L4.3
During a network partition, one half of your leaderless cluster can only reach 1 of the 3 replicas of a shard, with . What happens to reads and writes in that half, and which side of CAP did this configuration choose?
Recall Solution
With only 1 replica reachable and , both reads and writes fail in that half (cannot assemble a quorum of 2). By refusing to answer rather than returning possibly-stale/unconfirmed data, this configuration chose Consistency over Availability. The rule enforces consistency; the price under partition is that the minority side goes unavailable.
L5 — Mastery
One open-ended design problem tying every idea together.
Exercise L5.1
A global social app: 900,000,000 rows, write-heavy timeline feeds, users clustered by region (US, EU, APAC), must survive a whole-region outage, and users must read their own posts immediately after posting. Design the sharding + replication scheme and justify every choice against a specific concept.
Recall Solution
1. Shard key & strategy. Use hash(user_id) as the leading shard-key component (consistent hashing).
- Why not range on
user_idorcreated_at? Time-ordered keys create a write hotspot (L3.3). Hashing spreads write-heavy timelines evenly (Load Balancing). - Why consistent hashing not
mod N? A global app grows;mod Nwould force of 900M rows to migrate every time we add capacity (L2.1). Consistent hashing moves only .
2. Replication topology. Multi-leader, one leader per region (US/EU/APAC), each region's leader replicating to the others.
- Why multi-leader? Low-latency local writes and — crucially — survival of a whole-region outage (the other regions keep accepting writes).
- Cost accepted: cross-region write conflicts need resolution (last-write-wins with version vectors, or app-level merge).
3. Read-your-writes. Route a user's reads to the same region/leader that took their write, or within a shard's replicas use a quorum with .
- Why? Otherwise replication lag lets a user post and then not see their own post. Overlapping read/write sets (or sticky routing to their leader) guarantees they read the newest value.
4. Per-shard replica count. replicas per shard, defaulting to where strong consistency is needed, degrading to async/eventual for non-critical feeds.
- Why ? guarantees overlap and tolerates 1 replica down on each path (L4.2).
5. CAP stance. Under partition, timeline reads favour Availability (a slightly stale feed is fine), but "did my post save?" favours Consistency. Choose CAP per operation — exactly the parent note's point.
Summary of the mapping:
- Scale → consistent-hash sharding on
hash(user_id). - Region survival → multi-leader replication.
- No hotspots → hashed key, not time-ordered.
- Read-your-writes → sticky routing / quorum overlap.
- Copies × pieces → shard-replicas total.