Quick revision
The short answer
Each replica splits its key range into buckets, hashes each bucket's data into a leaf and builds parent hashes up to one root. The replicas compare roots, and wherever two hashes differ they compare the children, descending only into subtrees that differ, so with a few differences they exchange about (differing leaves) x (tree depth) x 2 hashes, roughly 120 hashes for 3 differences in a tree of a million leaves, instead of comparing every row, and then stream only the differing buckets. The trade-off is granularity: one changed row makes its whole bucket differ, and deeper trees are more precise but cost more memory and time to build.
What to remember
In this answer, we saw how replicas use Merkle trees to find differences cheaply. Each replica hashes the buckets of an identical key range into leaves and builds parent hashes up to a root. Comparing from the root down and descending only into differing subtrees exchanges about d log n hashes, around 120 for 3 differences in a million-leaf tree, after which only the differing buckets are streamed. The costs are granularity, since one changed row marks its whole bucket, the memory and build time of deeper trees, and differences caused by writes in flight, which are expected and rechecked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each replica splits its key range into buckets, hashes each bucket's data into a leaf and builds parent hashes up to one root. The replicas compare roots, and wherever two hashes differ they compare the children, descending only into subtrees that differ, so with a few differences they exchange about (differing leaves) x (tree depth) x 2 hashes, roughly 120 hashes for 3 differences in a tree of a million leaves, instead of comparing every row, and then stream only the differing buckets. The trade-off is granularity: one changed row makes its whole bucket differ, and deeper trees are more precise but cost more memory and time to build.
Why this question is asked
Replicas drift apart. A write is missed during a network blip, a node is down for an hour, a hint is lost. Finding exactly which rows differ between two copies of a terabyte without sending the terabyte is the problem Merkle trees solve, and Cassandra's repair, Git and certificate transparency logs all rely on them. Interviewers ask this because it is a clean example of trading a little computation for a huge reduction in network traffic. In this answer, we will recap what a Merkle tree is, build one over a key range, walk through the comparison, count the hashes exchanged, and look at the trade-offs.
1A Quick Recap of Merkle Trees
A hash function turns any amount of data into a short fixed-size value, for example 32 bytes with SHA-256, and the slightest change to the data changes the hash completely. A Merkle tree applies this in layers. Each leaf holds the hash of one block of data. Each inner node holds the hash of its children's hashes put together. At the top, a single root hash summarises everything below it.
The useful property follows directly: if two trees built the same way have the same root hash, all the data under them is the same (barring a practically impossible hash collision). If the roots differ, at least one leaf differs, and the path of differing hashes from the root leads straight to it.
2Building the Tree Over a Key Range
For replica comparison, each replica takes the same key range, for example one token range in Cassandra, and splits it into the same buckets by key or token. Bucket 0 covers the first slice of the range, bucket 1 the next, and so on. For each bucket, the replica hashes all the rows in it, in a fixed order, into one leaf hash. Then it builds parents: hash(leaf 0 + leaf 1), hash(leaf 2 + leaf 3), and upward until there is one root.
Notice the requirement hidden in "the same buckets": both replicas must build their trees over identical ranges with identical bucket boundaries and the same hash function. Otherwise the leaves describe different data and the comparison means nothing. A tree with 2 to the power 20 leaves, about a million buckets, has a depth of 20.
3Comparing Two Trees
The replicas compare their roots first. Equal roots mean the whole range matches and they are done, after exchanging a single hash. If the roots differ, they exchange the two children's hashes and compare each pair. A matching pair means that entire half of the range matches, and they skip it. A differing pair means they descend into it and repeat. When they reach the leaves, the differing leaves identify exactly which buckets need repair.
| Level | Hashes compared | Result |
|---|---|---|
| Root | 1 pair | differ: descend |
| Children (buckets 0-3, 4-7) | 2 pairs | 0-3 equal: skip; 4-7 differ |
| Under 4-7 (buckets 4-5, 6-7) | 2 pairs | 6-7 equal; 4-5 differ |
| Leaves 4 and 5 | 2 pairs | bucket 5 differs: stream it |
In the 8-bucket example, the left half of the range is skipped after one comparison, and only one path of hashes is followed down to bucket 5.
4How Many Hashes Are Exchanged?
With only a few differences, the number of hashes exchanged is about the number of differing leaves times the depth of the tree, times two for the pair of children at each step. Written in terms of d differing leaves and n leaves, that is O(d log n). Take a tree of about a million leaves, depth 20, and 3 differing buckets: roughly 3 x 20 x 2 = 120 hashes, a few kilobytes. Comparing leaf hashes directly would mean sending a million hashes, about 32 MB, and comparing every row would mean sending the data itself.
Once the differing buckets are known, only their rows are streamed between the replicas and reconciled, for example by keeping the newest version of each row. If the two replicas differ a lot, the comparison descends into most subtrees and the advantage shrinks, but then nearly everything needs streaming anyway.
5The Trade-Offs: Granularity and a Moving Target
The leaf is the unit of repair. A leaf covers all the rows in its bucket, so one changed row makes the whole bucket differ and the whole bucket gets compared or streamed. If a bucket holds 10,000 rows, a single missed write causes 10,000 rows to be sent. Deeper trees make buckets smaller and repairs more precise, but every level doubles the number of nodes, so they cost more memory and more time to build, because building requires reading and hashing all the data. Systems therefore cap the depth; Cassandra, for example, limits its repair trees to a fixed maximum depth and splits large ranges into several trees (check your version for the exact limits).
The second trade-off is that data keeps changing while trees are built. Each tree is a snapshot of one moment, and the two replicas never build theirs at exactly the same instant. A write that was applied to one replica just before its tree was built and to the other just after will show up as a difference even though nothing is wrong. Such differences are expected; they are either repaired harmlessly or rechecked in the next round.
6Follow-up questions
Why not just compare a hash of every row?
That would mean sending one hash per row, billions for a large table, which is far more than the hundreds a Merkle tree needs when replicas mostly agree. The tree lets whole subtrees be ruled out with a single comparison.
How does Git use the same idea?
Every Git object is named by the hash of its content, and a tree object contains the hashes of its files and subtrees, so a commit's hash covers the whole snapshot. Two repositories can tell which objects they are missing by comparing hashes from the top down, without comparing file contents.
What makes building the tree expensive in a database?
Building it requires reading every row in the range and hashing it, which is a full scan of the data. That is why repairs are scheduled, split into smaller ranges and throttled, and why incremental approaches that only consider data written since the last repair are attractive.
7Conclusion
In this answer, we saw how replicas use Merkle trees to find differences cheaply. Each replica hashes the buckets of an identical key range into leaves and builds parent hashes up to a root. Comparing from the root down and descending only into differing subtrees exchanges about d log n hashes, around 120 for 3 differences in a million-leaf tree, after which only the differing buckets are streamed. The costs are granularity, since one changed row marks its whole bucket, the memory and build time of deeper trees, and differences caused by writes in flight, which are expected and rechecked.
Open with
Each replica splits the same key range into the same buckets, hashes all the rows in each bucket into a leaf, then builds parent hashes from pairs of children up to a single root.
Then, if they want more
To compare, the replicas exchange roots first; if the roots match, the whole range matches and they're done after one hash. If not, they exchange the two children's hashes, skip any subtree whose hashes match, and descend only where they differ, until they reach the differing leaves, which identify the buckets to repair.
Then only those buckets' rows are streamed and reconciled. With a few differences, the exchange is roughly the number of differing leaves times the tree depth, times two, so O(d log n).
For a tree with about a million leaves, depth twenty, and three differing buckets, that's around a hundred and twenty hashes instead of a million leaf hashes or every row. The trade-off is granularity: a leaf covers many rows, so one changed row makes its whole bucket differ and the whole bucket is compared or streamed.
Deeper trees are more precise but cost more memory and more time to build, since building means reading and hashing all the data, which is why Cassandra caps tree depth.
Also, both sides must build over identical ranges with identical hashing, and with live data each tree is a snapshot, so differences from in-flight writes are expected and just rechecked or repaired harmlessly.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do two replicas use Merkle trees to find which parts of their data differ, and how...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/bf3dc258a8/