LESSON 07 · Anti-entropy

Repair replica drift

Compare data efficiently so long-lived divergence does not become permanent.

SYSTEM VIEW

The storage path

Diagram view
Hash comparisons—
Keys to examine—

Watch two replicas find the one range that differs out of a million keys, then repair it safely.

  1. Replica A holds order:42 at v2, but Replica B still has v1: the hint meant for B expired. The key is rarely read, so no read-time fix reaches it either. Comparing the two replicas key by key would mean sending all 1 million keys.

    Each replica
    1 million keys
    Key by key
    send all 1 million

    What changed

    • B missed v2: hint expired
    • No read touches order:42
  2. Anti-entropy, a background process, has each replica hash its range into a Merkle tree of 1,024 leaves. A leaf hashes about 1,000 keys and their values, each parent hashes its two children, and the root summarizes all 1 million keys in one hash.

    Leaves
    1,024
    Keys a leaf
    about 1,000
    Root
    all 1 million keys

    What changed

    • Merkle tree A
    • Merkle tree B
  3. Merkle tree A sends its root hash to Merkle tree B. The roots differ, so the replicas disagree somewhere. They compare the two children next: the left hashes match, which skips half the range, 500,000 keys, in one step.

    Compared
    root, then 2 children
    Skipped
    left half: 500,000 keys

    What changed

    • Roots differ
    • Left half skipped
  4. They follow only the differing right branch, comparing two child hashes at each of 10 levels: about 20 comparisons. The walk ends at one leaf, so about 1,000 keys are examined instead of 1 million. Were differences scattered across most leaves, the narrowing would fade.

    Levels
    10, two hashes each
    Examined
    about 1,000 keys

    What changed

    • One leaf of 1,024 differs
    • About 1,000 keys to examine
  5. Inside that leaf the replicas compare the keys’ versions and find order:42: Replica A holds v2, Replica B v1. The tree only found the difference; versions decide which value wins. Version v2 is newer, so A copies it to B, and the trees match again.

    Found by
    the Merkle trees
    Decided by
    versions: v2 is newer

    What changed

    • B holds order:42 v2
    • Trees match again
  6. While B was away, order:17 was deleted on A. Had A removed it, B’s v1 would look like new data, and repair would copy it back. So the delete writes a tombstone recording it and its version, v2. Repair sends it, and B drops v1.

    Tombstone
    order:17 deleted, v2
    Without one
    v1 is copied back

    What changed

    • Tombstone for order:17
    • order:17 v1 on B
  7. Cassandra keeps tombstones ten days by default. This time B stays down 14 days: A purges the tombstone on day 10, so B’s returning v1 looks like data A lacks, and repair copies it back. A node down past retention should be rebuilt, not rejoin.

    Tombstones kept
    10 days (Cassandra)
    B was down
    14 days
    Instead
    rebuild B

    What changed

    • Tombstone purged on day 10
    • order:17 v1 back on A
  8. Keeping tombstones longer covers longer outages, but costs disk and slows reads. Repair itself runs rate-limited, trickling along in the background, so foreground traffic stays healthy.

    Longer retention
    more disk, slower reads
    Repair
    rate-limited

    What changed

    • Longer retention: more disk
    • Repair rate-limited

Space plays · ← → step · Home resets

THE IDEA

How it works

Hints and read-time fixes reach only what they touch, so replicas can still drift when a hint expires or a key is rarely read. Anti-entropy is a background process that compares replicas responsible for the same range and copies what differs. Sending every key to compare would be wasteful. A Merkle tree hashes groups of keys and values upward: each leaf hashes a small key range, each parent hashes its children, and the root summarizes the whole range.

As an example, two replicas hold 1 million keys under a tree with 1,024 leaves, about 1,000 keys per leaf. Their roots differ, so they compare the two children. The left hashes match, which skips half the range in one step. Following the differing branch down 10 levels takes about 20 hash comparisons and ends at one leaf, so roughly 1,000 keys are examined instead of 1 million. The narrowing fades if differences are scattered across most leaves. The tree detects differences, but does not decide which value is authoritative; versions do that.

Repair also needs deletion metadata. If a delete simply removed the key, a replica that missed it would seem to hold new data, and repair would copy the value back. So a delete writes a tombstone, a marker recording the deletion and its version, retained long enough for every replica to learn of it. Cassandra’s default is ten days, and a node down for longer should be rebuilt rather than rejoin. Longer retention costs disk and slows reads. Rate-limit repair so foreground traffic remains healthy.

DECISION POINT

A replica returns after missing a delete, and its old value reappears. What was missing?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Merkle comparisons narrow the ranges that differ.
  • Tombstones and version choice make convergence safe.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 2
RecallQUESTION 01

What does a Merkle tree help replicas do?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 2 checkpoints answered correctly to complete this lesson.