Why Ethereum Uses a Merkle Patricia Trie for State
Suryansh Shrivastava3 min read·Just now--
Ethereum maintains two kinds of state:
- A mapping of accounts (address → nonce, balance, code, etc.)
- A mapping of storage slots for each contract (key → value)
At an abstract level, both are just key–value stores.
So a natural question arises:
Why doesn’t Ethereum just store all this as a simple flat key–value database?
The answer lies in verification.
The Problem with Flat State
Imagine storing Ethereum’s entire state as a flat map:
address → account
(storageKey → value)This is extremely efficient for reads and writes.
But Ethereum isn’t just a database — it’s a distributed system where every node must independently verify correctness.
To verify the state, we need a single cryptographic commitment (a hash) representing all data.
With a flat structure, that means:
stateRoot = hash(all key-value pairs)Now here’s the problem:
Every time any value changes, you’d need to recompute the hash of the entire dataset.
For millions of accounts and storage entries, this becomes computationally infeasible.
First Idea: Merkle Trees
To avoid hashing everything from scratch, we can organize the data into a Merkle tree.
- The leaves store actual data
- Each internal node stores the hash of its children
- The root acts as a commitment to the entire dataset
Now, when a value changes:
- Only hashes along the path from the leaf to the root need to be recomputed
- This reduces the cost from O(n) to O(log n)
This is a huge improvement.
The Catch: Insertions and Deletions
Merkle trees work best when the dataset is static or append-only.
But Ethereum’s state is highly dynamic:
- Accounts are created and deleted
- Storage slots are updated constantly
In a standard Merkle tree:
- Data is split into fixed chunks
- Inserting or deleting data shifts these chunks
- This invalidates large portions of the tree
So while updates are efficient, structural changes are not.
Second Idea: Patricia Tries
To solve this, we can organize data based on the keys themselves, rather than arbitrary chunking.
Enter the Patricia trie (a compressed prefix tree):
- Each key defines a path in the tree
- Nodes represent shared prefixes of keys
- Insertions and deletions only affect a single path
This means:
- No global reshuffling of data
- Updates remain localized
- Operations stay O(log n)
The Final Design: Merkle Patricia Trie
Ethereum combines both ideas:
- Patricia trie → efficient structure for dynamic key-value data
- Merkle tree → cryptographic verification
The result is the Merkle Patricia Trie (MPT).
With this structure:
- Every piece of state contributes to a single state root
- Any modification updates only a logarithmic number of nodes
- Inclusion and exclusion proofs can be generated efficiently
One Subtle but Important Detail
Ethereum doesn’t insert keys directly into the trie.
Instead, it hashes them first:
path = keccak256(key)This has two important benefits:
- Uniform distribution → avoids unbalanced trees
- Security → prevents adversarial key patterns
Putting It All Together
The Merkle Patricia Trie gives Ethereum:
- Efficient updates (O(log n))
- Efficient verification (O(log n) proofs)
- A single cryptographic commitment to the entire state
Most importantly:
It allows every node in the network to independently verify state transitions without trusting anyone else.
Intuition
If you had to summarize it in one line:
Ethereum’s state is stored in a Merkle Patricia Trie because it provides a cryptographically verifiable, efficiently updatable key–value structure.