Topic 131 / Specialist
Anti-entropy: Merkle trees, read repair, hinted handoff
Chapter 8.2 · Consistency Theory
What and why
Quorums (→ 130) let an operation succeed without every replica participating, leaving some replicas out of date on purpose — sloppy quorums and partitions leave others missing writes entirely. Anti-entropy is the background process that finds and fixes those divergences without waiting for a client to notice: read repair (fix a replica the moment a read reveals it's stale), hinted handoff (temporarily hold a write meant for an unreachable node and deliver it once that node returns), and Merkle trees (a hash-tree structure letting two replicas find exactly which key ranges differ without comparing every key). Builds on: → 130 (quorums).
Diagram
Replica A tree Replica B tree
H_root (≠) H_root (≠)
/ \ / \
H_L (=) H_R (≠) H_L (=) H_R (≠)
/ \ / \
H_R1(=) H_R2(≠) H_R1(=) H_R2(≠) ← only this leaf range needs syncing
How it works
- Read repair: on a quorum read, if the R responding replicas disagree, the coordinator returns the most recent value and, in the background, writes it back to the stale replica(s) — fixed only for keys that happen to get read.
- Hinted handoff: when a write's designated replica is unreachable (sloppy quorum, → 130), a reachable node stores the write plus a "hint" of its true destination; once that node rejoins, the hint is delivered and discarded.
- Read repair and hinted handoff only fix keys that get read or written again — cold keys can stay divergent indefinitely without a separate mechanism.
- Merkle trees close that gap: each replica builds a tree of hashes over its key ranges (a leaf hashes a small key range; each parent hashes its children's hashes). Replicas compare root hashes first — equal roots mean identical ranges, nothing more to check.
- If roots differ, replicas walk down only the branches whose hashes disagree, narrowing the search each level, until the specific small key ranges that actually differ are found — then only those ranges are transferred, not the whole dataset.
- This runs as a periodic background job, independent of client traffic, so even never-read keys eventually converge.
Practical example
(illustrative). Two replicas each holding a large key range build a Merkle tree; comparing a handful of hashes level by level narrows a true divergence down to one small leaf range without a full key-by-key scan of either replica — only that narrowed range is transferred.
When to use it
- Use when: a leaderless, quorum-based store where no single node's log is the source of truth for what's missing — read repair and hinted handoff for the common case, Merkle trees for divergence in data that isn't being actively read.
- Avoid when: a leader-based system — a leader's replication log (→ 72) already tells followers exactly what they're missing, simpler than detecting divergence after the fact.
- Failure scenarios: relying on read repair alone and finding rarely-read keys drift silently for a long time; a hinted-handoff node that itself crashes before delivering its held writes, losing them unless hints are durably stored.
- Simpler alternative: for a small, leader-based cluster, a follower that fell behind simply re-syncs from the leader's log — no tree comparison needed.
Practice
(Mechanism kind — trace by hand; 20–30 min, no lab services). Given two 8-key replicas differing in exactly 1 key, build a 3-level Merkle tree (4 leaf ranges of 2 keys each) for both, compute which leaf hash differs, and show that only that one 2-key range needs comparison. Count total hash comparisons made versus the 8 key-by-key comparisons a naive diff would need. Done when your hash-comparison count is strictly less than 8 and you can point to the exact leaf range transferred.
Key takeaway
Anti-entropy is background repair for divergence quorums allow on purpose — read repair and hinted handoff fix what's touched; Merkle trees find what isn't, without a full scan. Done when you can: explain why cold keys need Merkle trees even when read repair and hinted handoff are both running. Used next in → Capstone.
References
DeCandia et al., "Dynamo: Amazon's Highly Available Key-value Store" — see
references.md.