4.4.27 · D3Databases

Worked examples — Distributed databases — sharding strategies, replication

4,174 words19 min readBack to topic

This page leans on tools you already met:

  • mod (the remainder operator): is "the remainder when is divided by ". It answers "which of the numbered bins does fall into?" — that's exactly the question hash sharding asks.
  • The ring of Consistent Hashing: a circle of positions where a key belongs to the first node clockwise.
  • The pigeonhole principle behind quorums: if two sets are big enough, they must share an element.

The scenario matrix

Every problem in this topic is one of these cells. Read it top to bottom before any example.

# Case class What makes it "special" Covered by
C1 Naive mod N resharding measure fraction that moves as Ex 1
C2 A specific key under mod reshard sign of "does THIS key move?" (add AND remove) Ex 2
C3 Consistent-hashing add node one arc moves — interior AND wrap Ex 3 (figure)
C4 Consistent-hashing remove node mirror case — interior AND wrap Ex 4 (figure)
C5 Quorum — the healthy default strictly Ex 5
C6 Quorum — degenerate boundary: overlap can be zero Ex 6
C7 Quorum — limiting or one extreme node = no fault tolerance Ex 7
C8 Range-shard hotspot (zero-spread input) all keys land in one bin Ex 8
C9 Real-world word problem pick topology + quorum for a business Ex 9
C10 Exam twist "safer replicas" trap — bigger breaks quorum Ex 10

C1 — Naive mod N resharding, the whole dataset

Figure — the bin lottery. The bar chart below draws every possible new bin as a column of equal height (each new bin is equally likely for a random hash). Exactly one green column is the key's old bin — the "stays" outcome. All the red columns are "moved" outcomes. Because there are red columns for every green, you can see that is just "red columns over total columns".

Figure — Distributed databases — sharding strategies, replication

C2 — Does this specific key move? (add AND remove)


C3 — Consistent hashing: add a node (interior AND wrap)

Figure — the ring with the stolen arc highlighted. The left panel shows the interior insert: the four blue nodes sit at , and the red arc is the slice the new green node E now owns. The right panel shows the wrap insert: green node F at grabs the red arc , while the tiny slice (drawn continuing toward the yellow -marker at the top) still walks clockwise across to node . The yellow arrow shows the clockwise direction; the yellow dot at the top is position , the wrap point.

Figure — Distributed databases — sharding strategies, replication

C4 — Consistent hashing: remove a node (interior AND wrap)

Figure — the ring after a node dies. The left panel marks node with a red ✗ and colours its orphaned arc green; a green arrow shows those keys flowing clockwise to the next survivor, node . The right panel marks the top node dead; its orphaned arc (green) has no survivor before the top, so the green arrow curves clockwise across the yellow -marker to reach node . Both panels are the same clockwise rule — only the receiving node differs.

Figure — Distributed databases — sharding strategies, replication

C5 — Quorum: the healthy default


C6 — Degenerate boundary:


C7 — Limiting values: and


C8 — Range-shard hotspot (zero-spread input)


C9 — Real-world word problem


C10 — Exam twist: "more replicas = safer"


Recall

Recall Every cell, one line each

Fraction moved, naive mod, ? ::: About — nearly all keys. What does mean in words? ::: The fraction of keys forced to change shards, between 0 (none) and 1 (all). Why is ? ::: After the change there are bins; a random hash lands back in its old bin only 1 time in . Does a key move under mod reshard? ::: Only if — check BOTH grow and shrink. The single rule for who owns a key on a ring? ::: First surviving node clockwise from the key, crossing position 0 if needed — same for add and remove. On a ring, adding a node moves which keys? ::: Only the one arc between the new node and its clockwise neighbor — including when that arc crosses . What is the half-open arc ? ::: Positions from included up to excluded, so no position is owned by two arcs. Why is the benchmark arc size? ::: With evenly spaced nodes the ring splits into equal arcs, so one new arc is exactly . What are and ? ::: The write set (the replicas that got the value) and the read set (the replicas we query) — both subsets of the replicas. The strict quorum rule for read-your-writes? ::: (equality is NOT enough). Write-side vs read-side fault tolerance? ::: Write survives down, read survives down; system tolerates of the two. Why does adding replicas alone break consistency? ::: It raises , so fixed may fail .