Grokking the System Design Interview, Volume II
Vote

0% completed

​

Anti-entropy Through Merkle Trees

What are Merkle trees?

Merits and demerits of Merkle trees

Anti-entropy Through Merkle Trees Explained: Catching Silent Replica Drift, Comparing Hashes Down Differing Branches, and the Cost of Cluster Changes (Video)

This lesson covers anti-entropy: the background process Dynamo uses to catch replicas that have quietly drifted out of sync. It repairs them using a structure called a Merkle tree.

Vector clocks catch a conflict the moment a client reads the data. But a replica nobody reads can drift for a long time before that happens, and vector clocks never get a chance to run on it.

What we want is a way to catch that drift and repair it in the background, without waiting for a read. That means comparing two copies of the same range of data, and finding exactly where they differ.

What are Merkle trees?

A replica can hold a lot of data. Comparing it byte by byte would send far more data across the network than the actual difference could justify.

Dynamo solves this with a Merkle tree: a binary tree of hashes. Each leaf node hashes a small portion of the data, and each internal node hashes its two children.

Image

Comparing two Merkle trees follows a simple recursive rule:

  1. Compare the root hashes of both trees.
  2. If they match, stop. The two ranges are already identical.
  3. If they do not match, recurse into the left and right children.

That rule is why replicas never need to send a whole range just to find a difference. They walk down only the branches that differ, so the data exchanged is limited to the actual gap between the two copies.

No central node builds or stores these trees. Each node keeps a separate Merkle tree for every key range it hosts, which means one tree for each virtual node. Two nodes that hold replicas of the same key range compare the trees for that range. They first exchange the root hashes. Then they exchange only the hashes of the branches that differ. Because each tree covers exactly one key range, a node that gains or loses a range must also build or drop the tree for it.

Merits and demerits of Merkle trees

The advantage is that each branch can be checked on its own, without either side downloading the whole tree or the whole dataset. That keeps both the data transferred and the disk reads during anti-entropy small.

The disadvantage shows up when the cluster changes. Adding or removing a node moves many key ranges at once, and each move forces the trees covering those ranges to be recalculated.

Rohit Bhanot

Rohit Bhanot

· 6 months ago

Vector Clocks do NOT resolve conflicts, they just detect the conflict, the onus of resolution lies on the client !

J

Junaid Effendi

· 4 years ago

Where does the Merkle trees generation/search happens? Does all node store it in-memory? Is there a separate dedicated node that does that?

Show 1 reply

Reading Progress

0%


Vote for new content

On This Page

What are Merkle trees?

Merits and demerits of Merkle trees