In this lesson · topic 6 of 7
Topic 6
Merkle trees, expiring hints, and deleted data coming back
6 min read
Read repair fixes what is read. Hinted handoff fixes what a stand-in node was holding. Neither covers everything, and the gaps are where leaderless stores lose data in practice.
Finding the difference cheaply
Two replicas each hold about a million keys in a range, and a few differ. Sending every key and value across to compare would move gigabytes. Sending one hash per key would still be a million hashes.
A Merkle tree does better. Hash each small slice of the range to get the leaves. Hash each pair of leaves to get their parent, and repeat to the root. Two replicas that hold the same data have the same root hash, so one comparison proves they agree.
If the roots differ, compare the two children. Descend only into the side that differs. With 2^20 leaves, about a million, a single differing slice is found after 20 levels, around 40 hash comparisons. Then the replicas exchange only the keys in that slice. Dynamo uses this for its background anti-entropy, and Cassandra’s repair does too, building a tree for each key range being repaired.
The cost is building the tree, which reads the data. That is why repair is run on a schedule, not on every request.
Hints expire
A stand-in node holds a hint for an owner that was unreachable. It cannot keep hints forever, because a node that stays dead would make the stand-in’s disk fill up. Cassandra keeps hints for a replica for up to three hours of downtime by default. A node that is down for longer is not caught up by hints at all. It needs a repair run before it can be trusted again.
Hints are also a best effort. If the stand-in crashes before delivering, they are lost.
Deletes that undo themselves
A replica cannot simply erase a deleted value. If it did, the replicas that had not heard about the delete yet would look newer, and repair would copy the value back. So a delete is written as a marker, a tombstone, which is replicated like any write.
Tombstones cannot live forever either. Cassandra keeps them for gc_grace_seconds, ten days by default, and then drops them in compaction.
Now run this sequence with three replicas:
- Replica C goes down on day 0.
- On day 1, a key is deleted with a write quorum of A and B. Both hold a tombstone.
- On day 11, the tombstones on A and B pass the grace period and are removed. C comes back still holding the old value.
- Nothing anywhere says the key was deleted, and C holds a value. The next repair, or a read that includes C, copies it to A and B.
The deleted row is back. Nobody wrote it. Operators call these zombies.
Predict first
The same logic explains the guidance for a replica that was down longer than the grace period. Do not simply bring it back. Wipe it and rebuild it from the others, or repair it first.
Sources
- Giuseppe DeCandia and colleagues, Dynamo: Amazon’s Highly Available Key-value Store, ACM SOSP, 2007. Section on anti-entropy using Merkle trees.
- The Apache Cassandra project, Repair and Hints.
- The Apache Cassandra project, Compaction overview, on tombstones and
gc_grace_seconds.