Cryptographic hash functions are useful for more than checking whether two individual files have changed. They can also help compare large datasets, identifying which records have been added, modified or removed without having to examine every record individually. This becomes particularly important when dealing with databases, backups or distributed collections containing millions of records, where a full comparison can be expensive in terms of processing time and resources.
One particularly useful structure for this purpose is the Merkle tree, named after computer scientist Ralph Merkle. By organizing individual record hashes into a hierarchy, Merkle trees make it possible to compare entire datasets efficiently and progressively narrow down differences to the specific records that have changed.
Imagine a database table containing two million customer records. You have saved a backup and now want to determine which records have changed since that backup was created. Comparing every record individually would require reading and checking the entire dataset. A Merkle tree offers a more efficient way to approach this problem.
The first step is to build a tree representing the original dataset. You begin by sorting the records by primary key so that their order is consistent. You then calculate a cryptographic hash, such as SHA-256, for each record. These hashes form the leaves, or bottom level, of the tree.
Next, you take the hashes in pairs, concatenate each pair and calculate a new hash from the result. These new hashes become the parent nodes of the leaves. You repeat the same process, combining pairs of parent hashes to create successive levels, until only one hash remains. This final value is called the Merkle root. It represents the contents of the entire dataset: changing even one record will normally produce a different root hash.
The result is a hierarchy in which every node represents a group of records. The leaves represent individual records, while the higher levels represent progressively larger groups. The Merkle root sits at the top and summarizes the whole collection.
Great, but how does this help identify changes?
Comparing Datasets
So, now you have your backup and its related Merkle tree. Note that the Merkle tree can be easily saved to a relational table made of just three columns: Level where 0 is the leaf and 1 is the level immediately superior, Index ranging from 1 to N counting the nodes at the same level and Hash to be the hashed value of the record or the combination of records.
To make a quick comparison, you build a second Merkle tree from the current database and compare its root with the root saved from the backup. If the two roots match, there is strong evidence that the datasets are identical. If they differ, you compare the hashes of the corresponding branches beneath them. Matching branches can be ignored, while branches with different hashes are examined further. By continuing down the tree, you eventually reach the individual records that have changed.
This structure also makes updates efficient. When an existing record changes, you need to recalculate its leaf hash and the hashes of the parent nodes along the path to the root. In a balanced binary tree, this requires approximately logarithmic work relative to the number of records. For a dataset of two million records, that means roughly 22 hash calculations rather than rebuilding the entire tree.
Facts of Merkle Trees
Merkle trees are also reasonably compact. A tree representing two million records contains approximately two million leaf hashes and another two million internal-node hashes. With SHA-256, each hash occupies 32 bytes, giving approximately 128 MB of raw hash data, excluding storage overhead. The tree can be persisted in a relational database using a simple table containing the level, the node’s index within that level and its hash value.
Merkle trees have become particularly important in distributed computing. Git, for example, uses content hashes and tree objects to represent repository contents in a structure commonly described as a Merkle directed acyclic graph. This allows Git to identify objects by their contents and detect changes throughout a repository.
Blockchains use Merkle trees to summarize the transactions included in a block. Instead of requiring someone to download and inspect every transaction, a Merkle proof can demonstrate that a particular transaction belongs to the block using only a small number of hashes. The Merkle root is stored in the block header, providing a compact commitment to the block’s contents.
Summary
The underlying principle is simple but powerful: by combining individual hashes into a hierarchy, it becomes possible to verify large collections of data efficiently. Merkle trees are a particularly elegant example of how a concept originally developed for data retrieval evolved into an essential tool for modern computing.