Skip to content

Anti-entropy repair with Merkle trees

A background process in which replicas compare hash trees built over the same key range and stream just the sub-ranges whose hashes differ, catching missed writes that hints and read repair left behind.

On this page

What it is

Anti-entropy is the periodic work of bringing replicas of the same data back into agreement after some of them missed writes. The Dynamo paper describes it as a replica synchronization protocol for permanent failures, the cases hints don't cover, and picks Merkle trees as the way to find the differences cheaply. A Merkle tree is a hash tree: each leaf holds the hash of one key's value, and each parent holds the hash of its children, so the root summarizes the whole range. Cassandra describes the same structure as a hierarchy of hashes and uses it for its repair process.

The comparison runs top down. Two replicas that host the same key range exchange the root hashes of their trees for that range. If the roots match, the leaves beneath them match too, and there's nothing to send. If they differ, the replicas exchange the hashes of the children, keep descending along the branches that disagree, and stop at the leaves, which name the keys that are out of sync. Because hashes travel instead of data, and a matching subtree is skipped without reading its rows, both network transfer and disk reads stay low when most of the range already agrees.

yesnoreplayedexpiredWrite arrivesReplica up?StoredHint parkedCaught upStill behindRead repairAnti-entropy
Hints cover short outages, read repair fixes whatever gets read, and anti-entropy repair catches everything else.

Repair sits behind two cheaper mechanisms. With hinted handoff, a coordinator that can't reach a replica stores the write locally as a hint and replays it when the replica comes back; in Cassandra 5.0 new hints are kept for a downtime window that defaults to 3 hours, and a replica that returns inside the window gets its pending hints applied. With read repair, a coordinator that sees replicas disagree during a read updates the stale ones, which takes some work off the anti-entropy process; Cassandra's read repair is blocking by default and doesn't run for reads at consistency level ONE, since a single response already satisfies the request. Cassandra's own repair documentation calls hints a best effort that may not deliver all of the writes a node missed, and warns that the leftover gaps can turn into data loss as nodes are replaced or tombstones expire. Anti-entropy repair exists for those leftovers.

Leaf coversCost to build and holdWhat one differing row costs

One row

Highest: a hash per row, and the tree is large

That row is sent

Around 100 partitions (ScyllaDB 3.0's sub-range)

Lower: one checksum per sub-range

All 100 partitions are sent, and the data is read twice: once to checksum, once to stream

A capped tree over too many partitions (Cassandra)

Bounded by the tree's maximum size

Each leaf stands for a wide range, so a mismatch overstreams matching data

The table shows the tuning knob this page is about: how much data a leaf stands for. Cassandra caps the size of a Merkle tree, so a repair that covers too many partitions ends up with leaves that each represent a large slice of data and streams more than it needs to; its auto repair scheduler limits a repair assignment to 1,048,576 partitions by default to keep leaves narrow. ScyllaDB went the other way in 3.1 and moved from checksums over roughly 100 partitions to a checksum per row plus set reconciliation, so a mismatch sends the differing rows and nothing else. In its three-node benchmark with a billion rows per node and 0.1% of rows differing, repair time fell from 96.4 minutes to 14.2.

In Cassandra 5.0, repair doesn't run on its own by default, because it can generate heavy disk and network traffic; an operator runs nodetool repair, or, from 5.0.8 on, the auto repair scheduler can be enabled to run it on an interval. The default is an incremental repair, which works on data not yet marked as repaired by an earlier run; --full hashes the whole data set again. A node repairs the token ranges it holds, not the cluster, and the -pr flag restricts it to the ranges where it's the first replica so that running it on each node in turn covers the cluster once instead of several times. --preview builds and compares the trees and reports how much would be streamed without streaming anything; it still pays the hashing cost, but it gives you a measure of how far replicas have drifted.

When to use it

Reach for anti-entropy repair whenever a replica may have missed writes that no hint will bring back: a node that was down longer than the hint window, or hints that were lost with the node holding them. The Dynamo paper is explicit that hinted handoff works best when membership churn is low and failures are short, and that longer outages are what the anti-entropy protocol is for.

Run it on a schedule tied to deletes. Cassandra keeps a tombstone for a grace period, 10 days by default, and its documentation advises repairing each node at least once per 7 days so that no unrepaired data outlives that period; a replica that missed the delete would otherwise hand the row back to the others once the tombstone is gone. Since Cassandra 6.0, backported to 5.0.8, an auto repair scheduler can run full, incremental and preview repairs on an interval, and still asks you to keep that interval shorter than the grace period.

When not to

Don't propose a repair run as the first response to an outage shorter than the hint window. The coordinator has usually parked the missed writes as hints and will replay them, so a repair would read and hash the node's data for a difference hints are about to close. Hints are best effort, though, so the regular scheduled repair stays in place as the backstop; you don't start an extra one for each short blip. In Cassandra 5.0, repair is heavy on disk and network I/O, which is why it is run by an operator or by a scheduler you configure rather than kicking in on its own after each outage.

Don't repair a replica that has lost all of its data. With nothing in common, hash comparison finds that all ranges differ and the result is a full copy anyway, with the hashing on top; ScyllaDB's benchmark of a node with zero data took about 37 minutes with the 3.1 algorithm, and its engineers point to the node replacement procedure as the better choice for that case. Use the replacement procedure for the node instead, and repair afterward if you need to.

Be careful when key ranges move often. A node keeps a separate tree for each key range it hosts, so when a node joins or leaves and ranges change hands, the trees for the new ranges have to be built before anyone can compare them, and the hashing work lands on a cluster that is already busy moving data. Dynamo's later partitioning schemes divide the hash space into equal-sized partitions; our reading is that fixed partition boundaries are what let a tree outlive a membership change. And in a system with a single leader and an ordered log, a lagging follower usually catches up by replaying the log from its last position; there a hash comparison is more often a secondary check for corruption or divergence than the main catch-up path.

Compared

MechanismWhen it runsWhat it catchesPick it when

Hinted handoff

At write time, replayed when the replica returns

Writes missed during a short outage, within the hint window (3 hours by default in Cassandra 5.0)

Outages are short and churn is low; it's the cheapest layer and runs without an operator

Read repair

During a read that finds replicas disagreeing

Stale values on keys read at a consistency level that contacts more than one replica

Hot keys must converge fast; it does no work on keys nobody reads

Incremental Merkle repair

On a schedule, by operator or scheduler

Drift in data not yet marked repaired

Regular maintenance; it's Cassandra's default because it skips already-repaired data

Full Merkle repair

On a schedule or on demand

Any difference, including in data marked repaired

Validation shows repaired data out of sync, or you need one pass that trusts nothing

Replace the node

On demand

A replica with little or no data in common

The node lost its disk; copying everything beats hashing everything first

In a picture

yesnodescendReplica A: roothashRoots equal?Replica B: roothashRange in syncSwap child hashesDiffering leafStream that slice
Two replicas compare the roots of their trees for a shared key range and descend along mismatching branches until the differing leaves are found.

How to say it in an interview

The common mistake

Quiz

Two replicas exchange the root hashes of their Merkle trees for a shared key range and the roots are equal. What happens next?
A repair covers so many partitions that each leaf of the capped tree stands for thousands of rows. One row differs. What does the repair send?
Why does Cassandra's documentation ask you to repair each node within the tombstone grace period?
A replica was unreachable for 40 minutes and the hint window is 3 hours. Which mechanism brings it up to date for the writes it missed?

Questions that use this

Hear it in

Related

Sources

  1. Insights from paper (Part II): Dynamo, Amazon's Highly Available Key-value Store
  2. Apache Cassandra 5.0 documentation: Repair
  3. Apache Cassandra 5.0 documentation: Hints
  4. Apache Cassandra 5.0 documentation: Read repair
  5. Apache Cassandra documentation: Auto Repair
  6. Apache Cassandra documentation: Dynamo (architecture)
  7. ScyllaDB Open Source 3.1: Efficiently Maintaining Consistency with Row-Level Repair
Sign in to report a mistake