Read Repair, Anti-Entropy, and Merkle Divergence Checks

LESSON

Consistency and Replication

008 30 min intermediate

Read Repair, Anti-Entropy, and Merkle Divergence Checks

The core idea: Replicas do not converge because time passes; they converge because repair mechanisms detect disagreement, decide what should be copied or merged, and spend foreground or background capacity to make replicas agree again.

Core Insight

Imagine Harbor Point's leaderless store after the sloppy-quorum incident from the previous lesson. A watchlist update for watchlist:trader-17 landed on fallback nodes while two home replicas were unreachable. The write was accepted, the user kept working, and the system now has repair debt.

When the home replicas return, some keys are current, some are stale, and some temporary hints may fail or expire. A hot key might heal quickly because clients keep reading it. A cold key might stay wrong for weeks if no one looks at it and the system has no background comparison process.

Read repair and anti-entropy are two answers to that problem. Read repair heals divergence that a foreground read happens to expose. Anti-entropy proactively compares replicas in the background, often using Merkle-style summaries so the system can find mismatched ranges without copying every object.

The trade-off is where the system pays for healing. Read repair puts some work on the read path and favors hot data. Anti-entropy puts work into background disk, CPU, and network activity and covers cold data. Real replicated stores often need both because availability-first writes create disagreement faster than one repair path can clean it all up.

Read Repair: Healing When a Read Finds Drift

Suppose the home replicas for watchlist:trader-17 should be A, B, and C. After the incident, they hold different versions:

A -> version 7
B -> version 7
C -> version 5

A client reads the watchlist at a consistency level that contacts multiple replicas. The coordinator receives both v7 and v5, compares version metadata, returns the newest safe answer to the client, and sends the fresher value back to the stale replica.

client read
   |
   v
coordinator asks A, B, C
   |
   +--> A returns v7
   +--> B returns v7
   `--> C returns v5
   |
   v
return v7 to client
repair C with v7

That is read repair. It is attractive because it uses real traffic to discover real disagreement. If a key is hot, the system gets many chances to notice stale replicas and heal them.

The limitation is just as important: read repair only wakes up for keys that are read. If Harbor Point has a rarely opened watchlist, an old dashboard configuration, or a historical preference record, read repair may never touch it. The system can look healthy for active users while carrying stale cold data quietly.

Anti-Entropy: Healing Without Waiting for Users

Anti-entropy is the background answer. Instead of waiting for a client to read a key, replicas compare their state and repair differences proactively.

A naive process could compare every key and value directly, but that would be too expensive at scale. Many Dynamo-style systems use Merkle trees or similar hash summaries. Each replica builds a tree of hashes over a key range:

range watchlist:0000..9999
├── 0000..4999 hash=a13
│   ├── 0000..2499 hash=77c
│   └── 2500..4999 hash=9be
└── 5000..9999 hash=f42
    ├── 5000..7499 hash=21d
    └── 7500..9999 hash=ab8

Two replicas compare the root hash first. If it matches, that range likely agrees and no object-level comparison is needed. If it differs, they descend into child ranges until they find the smaller ranges or keys that disagree.

compare root hash
   |
   +-- same: skip entire range
   |
   `-- different: compare child hashes
          |
          `-- repair mismatched ranges or keys

This is why Merkle-style anti-entropy is useful: it narrows the repair search. The system can detect that most of a replica is identical and spend bandwidth only on the parts that drifted.

Anti-entropy is especially important for cold data, long-lived replicas, and recovery after outages. It turns "eventually consistent" from a hope into a scheduled repair process.

Worked Example: Hot Watchlist, Cold Preference

Harbor Point sees two divergent records after a zone incident:

Record                         Traffic pattern       Repair risk
-----------------------------  --------------------  -------------------------------
watchlist:trader-17            read every minute      read repair likely catches drift
layout:trader-17:legacy-view    read once per month    read repair may never fire

For the hot watchlist, a normal user read can expose the mismatch and repair the stale home replica. The user may pay a little extra latency on that read because the coordinator compares versions and sends repair writes.

For the cold layout preference, no one may read the record soon. Without anti-entropy, one replica can remain stale indefinitely. The problem may surface later during a restore, regional failover, audit export, or a rare user session that lands on the wrong replica.

The repair plan should therefore name both paths:

hot data:
- use read repair when quorum reads reveal stale replicas
- track extra read latency and repair write volume

cold and broad data:
- run anti-entropy scans over replica ranges
- compare Merkle summaries
- alert on repair backlog and oldest unrepaired range

The product benefit is convergence. The operational cost is extra work: foreground read latency for read repair, and background CPU, disk, and network pressure for anti-entropy.

Designing Repair as an SLO

Repair needs an objective, not just a feature flag. Harbor Point can decide that watchlist data should converge within minutes after a home replica returns, while archival preferences can converge within hours. Those goals shape repair frequency, bandwidth limits, and alert thresholds.

Useful signals include:

Signal                              What it tells you
----------------------------------  ---------------------------------------------
read repair rate                    how often foreground reads find divergence
anti-entropy backlog                how much replica range comparison remains
oldest unrepaired hint or range      whether convergence is falling behind
bytes repaired per hour             cost of healing after failures
repair-induced read latency          user-facing cost of foreground repair

There is a real trade-off here. Aggressive repair reduces divergence windows but competes with production traffic. Conservative repair protects foreground latency but lets replicas disagree longer. The right setting depends on the data's consistency contract and the cost of stale reads.

This also prepares the next lesson. Repair can copy the newer version when one version clearly supersedes another. When replicas contain concurrent versions with different user intents, the system needs a conflict-resolution policy before it can know what "repair" should write.

Failure Modes

Assuming eventual consistency means passive convergence. Time alone does not heal replicas. Some mechanism must observe divergence and apply a repair or merge policy.

Relying only on read repair. Hot keys heal quickly, but cold keys can remain stale indefinitely if no background anti-entropy process scans them.

Treating anti-entropy as free background work. Merkle building, range comparison, and repair writes consume CPU, disk, and network. Poorly tuned repair can hurt the same workloads it is trying to protect.

Repairing without a conflict policy. If two versions are concurrent rather than stale-versus-new, the system must resolve or surface the conflict before blindly copying one value everywhere.

Resources

Key Takeaways

  1. Read repair is foreground healing: it fixes divergence that a client read happens to reveal.
  2. Anti-entropy is background healing: it compares replicas proactively so cold data does not stay stale forever.
  3. Merkle-style summaries make background repair practical by narrowing comparison to mismatched ranges.
  4. Repair has a cost and a policy boundary: it must be tuned against production load and paired with conflict resolution when versions are concurrent.
PREVIOUS Leaderless Replication, Sloppy Quorums, and Hinted Handoff NEXT Conflict Resolution and Convergence Policies