techdiagrams.net
← All posts

Merkle trees: how replicas find a needle of difference in a haystack of data

Suchait Gaurav· · 3 min read distributed-systemsdata-structuresreplication

Here's a problem every replicated datastore eventually faces: two replicas are supposed to hold the same 50GB key range, but one missed a few writes during a network blip. Which keys differ? The brute-force answer — ship everything and compare — costs 50GB of network for what might be three stale keys. The answer used by Cassandra, DynamoDB, Riak, and (in spirit) git and blockchains fits in one picture:

Two replicas comparing Merkle tree roots and descending to the single differing block

A real techdiagrams document — download the JSON and import it into the editor.

The structure: hashes of hashes

A Merkle tree is built bottom-up over the data:

The property that makes it magic: any change anywhere changes the root. Flip one bit in block 2 and h23 changes, so the root changes. The root is a tamper-evident, difference-evident fingerprint of the entire dataset — at the cost of one small hash.

The protocol: descend only where it differs

Anti-entropy repair between replica A and replica B becomes a conversation:

  1. Exchange roots. Equal? Done — the entire range is provably in sync, and that certainty cost ~32 bytes.
  2. Roots differ (our diagram: 9f3a… vs 8c1d…). Exchange the children. h01 matches — that whole subtree is now proven identical and never spoken of again. h23 differs.
  3. Recurse into the differing branch only. b3 matches, b2 differs.
  4. Sync block 2. Total network cost: a handful of hashes plus the one block that actually changed.

That's the headline: O(log n) hash exchanges to locate any difference, instead of O(n) data transfer to find it. The tree doesn't just detect divergence — it routes you to it.

Where you've already met this

The trade-offs nobody mentions

See it as a system, not a data structure

The diagram above treats the tree like an architecture — because in a running system, it is one: replicas, a comparison protocol, a repair path. Import it into techdiagrams.net, attach real numbers (how often do blips happen? how big is a block?), and the "which leaf granularity?" question becomes something you can actually reason about instead of copying a default.

Get the next post in your inbox

Engineering notes from building an architecture editor that runs — React traps, simulation internals, AI product patterns. No spam, unsubscribe anytime.