Unit 9 of 14 · about 4 hours · derive, implement

Background anti-entropy and Merkle trees for difference localization

Repair needs a path that does not depend on client reads or surviving hints. Merkle comparison narrows where to reconcile; it does not choose which conflicting state is correct.

You'll be able to

  • Derive how a balanced Merkle tree localizes a changed range without transferring every value.
  • Implement canonical version-aware hashing and compare its diff result with a full scan.
  • Repair cold keys while accounting separately for tree construction, comparison and data transfer.

Covers

Background anti-entropyMerkle trees for difference localization
1

Read

In this order, only the parts listed

0 of 2 done

  1. Dynamo (SOSP 2007): range-based replica synchronizationDynamo: Amazon's Highly Available Key-value Store (DeCandia et al., 2007)

    Section 4.7, Handling permanent failures: Replica synchronization

  2. HLD Handbook: Merkle tree construction and repairHLD Handbook - Consistency, Quorums, Replication and Anti-Entropy

    Theory: The divergence problem, Merkle trees, Full anti-entropy; Common Pitfalls: Thinking read repair alone is enough for cold keys

    Use the tree diagram after Dynamo. Skip deployment settings, numerical capacity examples and the XOR safety claim in Hash choice matters. Collision-resistant leaves do not make XOR position-binding: exchanging two leaves preserves their XOR. Use ordered child concatenation and domain-separated hashing instead.

2

Focus on

  • Both replicas need the same range boundaries, key ordering, serialization and tree shape. Otherwise matching content may produce different trees.
  • Hash keys, causal versions, values and tombstones, not just visible values. Two replicas can answer today's read identically yet differ in future merge behavior.
  • For a balanced tree with fixed leaf ranges, one differing leaf needs descent comparisons after trees exist. Building the trees still reads and hashes the covered state.
  • Equal cryptographic roots are evidence of equal encoded snapshots under a collision assumption. A live tree changing during comparison needs a snapshot or a retry policy.
Common mistakes · 3
  • Hashing an unordered container or concatenating variable-length fields without unambiguous encoding.
  • Calling a differing hash proof that the remote value should overwrite the local one.
  • Reporting tree-descent cost while excluding a full rebuild on every repair round.
3

Try it yourself

Closed-book, before checking anything

In Python 3, build two SHA-256 binary trees over eight fixed, ordered key slots. Encode each leaf canonically with its key, version and value; use a distinct prefix for leaves and ordered internal-node hashes. Change only slot 5 in the second snapshot. Compare the root, then both child hashes of each differing internal node: the comparison visits seven node pairs and identifies only slot 5. Compare that result with an independent full scan. Reconcile that leaf, rebuild the affected path, and verify matching roots. Repeat with a tombstone-only difference and with identical input serialized from different dictionary insertion orders.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why does a matching visible value not imply matching repair state?Answer

    The replicas may retain different causal contexts, siblings or deletion metadata. Those differences can change later conflict resolution and must be covered by the digest.

  2. 2.Can Merkle comparison recover a value lost from every replica?Answer

    No. It compares available states and finds differences. It cannot reconstruct information absent everywhere.

  3. 3.What happens when every leaf differs?Answer

    The traversal may visit the whole tree and transfer all differing ranges. Sparse-difference savings are not a worst-case sublinear guarantee.

  4. 4.Why is XOR of child hashes insufficient as a position-binding tree construction?Answer

    XOR is commutative, so swapping children preserves the parent without finding any hash collision. Ordered, domain-separated hashing commits to the structure instead.

You're done when

Your tree diff agrees with the full-scan oracle on value and metadata differences, and your cost report separates rebuilding, comparing and transferring data.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 14 more in the section

Also in this section