# How do two replicas find what's different without sending everything?

By Vishal Kumar · Published 2026-11-13 · Updated 2026-11-13
Source: https://fungyaan.com/p/merkle-tree-replica-diff
Tags: data-structures, merkle-tree, replication, anti-entropy, go

**The verdict, as we're about to see earned:** compare a **Merkle tree** of each side. Hash the data in small groups, hash pairs of those hashes, and so on up to one root hash. If the roots match, the replicas are identical, checked with 32 bytes. If not, descend only into the halves that differ. With a million records, finding one changed key exchanged 2 KiB, against 43 MiB for a hash per key. This is how Cassandra and the original Dynamo repair replicas, how Git and many sync tools spot changes, and how certificate transparency logs prove they haven't been altered.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | Two replicas of a million records, one key has drifted |
> | **Suspects** | Full copy (203 MiB), a hash per key (42.9 MiB) |
> | **Culprit** | Sending data to compare data |
> | **The fix** | Compare Merkle roots, descend only where hashes differ |
> | **Evidence** | 2 KiB to find one changed key, Go standard library |

## The case

Two replicas, a million records each, and a quiet suspicion that somewhere one key has drifted. The machines can't see each other's data. Shipping everything across to compare is the obvious move, and the expensive one. So how do two strangers agree they hold the same thing without showing it to each other?

I never get tired of the answer. They agree, with 32 bytes, that a million records are identical, and when they aren't, they corner the difference by asking a handful of yes-or-no questions.

The evidence is in [systems-data-structures](https://github.com/the-vishal-kumar/systems-data-structures/tree/main/merkle) (Go, standard library only).

## The method

The detective here is Ralph Merkle's hash tree, and its whole method fits in four steps:

```mermaid
flowchart TB
  R["root = H(A|B)"] --> A["A = H(L1|L2)"]
  R --> B["B = H(L3|L4)"]
  A --> L1["leaf 1 = H(keys in bucket 1)"]
  A --> L2["leaf 2"]
  B --> L3["leaf 3 (differs)"]
  B --> L4["leaf 4"]
```

1. Each key goes to a leaf by its hash (so both replicas agree on placement without coordinating).
2. A leaf's hash covers its keys and their value hashes, in order.
3. Each parent hashes its two children.
4. Compare roots. If they differ, exchange the two children's hashes, and recurse only into the children that differ. At the leaves, exchange the key lists of the differing leaves.

Say leaf 3 is the only culprit. The walk compares root, then A and B, then L3 and L4, and exchanges only leaf 3's keys. That's the whole trick, and it's a lovely one: a disagreement at the top tells you exactly which half to look in next, like a game of twenty questions played with hashes. Every answer rules out half the suspects.

## The clues: a million records, 200 bytes each

First, the suspects people usually reach for. A full copy: **203 MiB**. A list of every key with a hash of its value: **42.9 MiB**. Both in a single round trip. Now the Merkle walk, at two tree sizes:

| Keys that differ | 4,096 leaves: bytes / round trips | 65,536 leaves: bytes / round trips |
|---:|---:|---:|
| 0 | 32 B / 1 | 32 B / 1 |
| 1 | 11.6 KiB / 14 | **2.0 KiB** / 18 |
| 10 | 115 KiB / 14 | 15.2 KiB / 18 |
| 100 | 1.1 MiB / 14 | 129 KiB / 18 |
| 1,000 | 9.5 MiB / 14 | 1.1 MiB / 18 |
| 10,000 | 39.3 MiB / 14 | 8.3 MiB / 18 |

```mermaid
xychart-beta
  title "MiB sent with 10,000 changed keys"
  x-axis ["Full copy", "Hash per key", "4,096 leaves", "65,536 leaves"]
  y-axis "MiB"
  bar [203 , 42.9 , 39.3 , 8.3]
```

Every run found exactly the changed keys: one changed record in a million, found with 2 KiB. That gap is the moment to pause and admire.

## The fine print

- **Bytes vs round trips:** a deeper tree has smaller leaves, so fewer keys are exchanged per difference, but walking it takes more sequential round trips (18 instead of 14 here). Across data centres, 18 round trips can cost more time than a few extra kilobytes. Real systems exchange several levels per round trip to cut this down. If you're wondering why replicas drift in the first place, the [replication and sharding case](/p/replicas-partitioning-sharding) sets the scene.
- **A false lead when there are many differences:** at 10,000 changed keys out of a million (1%), the small tree exchanged almost as much as a hash list would. When replicas have drifted a lot, the clever walk stops paying; send the hash list, or the data.
- **Building the tree costs a full read** of the data. Systems either keep the tree updated as writes arrive, or rebuild it on a schedule (Cassandra builds trees during a repair, which is why repairs are I/O heavy).
- **Placement must be deterministic.** Both sides must put each key in the same leaf; hashing the key does that without coordination.

## Where this detective works

| Situation | How Merkle trees help |
|---|---|
| **Database replica repair** (Cassandra, Riak, DynamoDB-style stores) | Replicas exchange trees per key range and stream only the differing ranges |
| **File sync and backup** | Hash chunks of files; unchanged chunks are skipped (rsync uses rolling checksums; content-addressed backup tools like restic use chunk hashes) |
| **Verifying a backup or a copy** | Compare root hashes instead of reading both copies side by side |
| **Tamper-evident logs** | Certificate Transparency and Git: changing any record changes every hash above it, so a published root commits to all the data |

> **Remember this**
> - Matching roots prove a million records identical with 32 bytes.
> - Each hash comparison rules out half the suspects.
> - Deeper trees send fewer bytes but need more round trips, and heavy drift makes the walk stop paying.

## What I'd do on Monday

1. **Compare roots first**; most of the time replicas match, and that costs 32 bytes.
2. **Choose leaf count from expected drift:** more leaves when differences are few and data is large.
3. **Batch several levels per round trip** across slow links.
4. **Fall back to hash lists or full copies** when drift is large.

It's a rare data structure that repairs databases, syncs files and keeps public logs honest, all with the same few lines of hashing. Merkle described it in 1979, and it is still the standard tool. Case closed, and beautifully.

*Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U). Byte counts follow from the algorithm and repeat exactly. Code and full results: [systems-data-structures](https://github.com/the-vishal-kumar/systems-data-structures).*
