Sub topic 14
Repairing Divergence with Read Repair and Merkle Trees
This chapter assumes you know replication with N replicas, read and write quorums, and vector clocks from earlier chapters. It covers how replicas that drifted apart get back in sync.
Why replicas diverge
A write succeeds once W replicas acknowledge it. The other replicas may have been down, partitioned, or slow, so they miss the update. Hinted handoff covers short outages. It does not cover a lost hint, a node that was down for days, or a disk that lost data. You need repair mechanisms that do not depend on the original write path.
There are two common repairs. Read repair fixes data that clients actually read. Anti-entropy fixes everything else in the background.
Read repair
During a read, the coordinator asks R replicas for the key and compares their versions. If one replica returns a stale version, the coordinator sends it the newest one.
- The coordinator sends the read to the replicas in the preference list.
- It compares the returned versions, using vector clocks.
- It returns the winning value (or siblings) to the client.
- It writes the winning version back to any replica that was behind.
Read repair is cheap, because the data is already in hand. Its weakness is that it only repairs keys that get read. A cold key can stay under-replicated for a long time, and a second failure could then lose it.
Merkle-tree anti-entropy
Anti-entropy runs in the background and compares whole key ranges between replicas. Sending every key to compare would cost too much bandwidth. A Merkle tree avoids that. It is a tree of hashes: each leaf hashes a small bucket of keys, and each parent hashes its children.
Two replicas exchange root hashes. If they match, the range is identical and nothing else is sent. If they differ, they compare the children and descend only into subtrees that differ. They finish at the leaf buckets that differ, and they exchange only those keys.
The code below builds a simple Merkle root and finds the differing buckets.
import hashlib
def digest(data: str) -> str:
return hashlib.sha256(data.encode()).hexdigest()
def leaf_hashes(store: dict, buckets: int = 4) -> list:
groups = [[] for _ in range(buckets)]
for key in sorted(store):
index = int(digest(key), 16) % buckets
groups[index].append(f"{key}={store[key]}")
return [digest("|".join(g)) for g in groups]
def merkle_root(leaves: list) -> str:
level = leaves
while len(level) > 1:
level = [digest(level[i] + level[i + 1]) for i in range(0, len(level), 2)]
return level[0]
a = {"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}
b = {"k1": "v1", "k2": "OLD", "k3": "v3", "k4": "v4"}
la, lb = leaf_hashes(a), leaf_hashes(b)
print("roots equal:", merkle_root(la) == merkle_root(lb))
print("buckets to sync:", [i for i in range(len(la)) if la[i] != lb[i]])The bucket count must be a power of two for this simple pairing. Both replicas must also use the same bucketing function.
Practical details
- Each node keeps one tree per key range it owns, so replicas compare matching ranges.
- Trees must be updated as writes arrive, or rebuilt periodically. Rebuilding is expensive on large ranges.
- When a node joins or leaves, the key ranges change and the affected trees must be rebuilt.
- Anti-entropy should be rate limited so it does not compete with client traffic.
Choosing between them
| Mechanism | Trigger | Covers | Cost |
|---|---|---|---|
| Read repair | A client read | Keys that are read | Low, extra write on stale reads |
| Merkle anti-entropy | Background schedule | All keys in a range | Tree upkeep and periodic comparison |
Most systems run both. Read repair keeps hot data fresh, and anti-entropy catches the rest.
Exercise
A key was written with W=2 of N=3, and the third replica missed it. Nobody has read the key since. Which mechanism will eventually repair it, and why does read repair not help here?
Show answer
Merkle-tree anti-entropy will repair it, because it compares whole key ranges on a schedule and finds the stale replica without any client request. Read repair only runs when a client reads the key, so an unread key stays stale.