Book a free call

Why Hashmaps Matter in Blockchain: Fast State Lookups

Hashmaps let a blockchain find any account balance or contract value by its key quickly. Here is where they sit in a node and why they matter for speed.

Hertiv engineering teamReviewed by a senior engineer
7 min read
Why Hashmaps Matter in Blockchain: Fast State Lookups
On this page

Short answer

A hashmap is a data structure that stores key-value pairs and retrieves any value from its key in near-constant time. Blockchains use hashmaps to hold account balances, contract storage slots and transaction indexes, so a node can answer what a balance is without scanning the whole chain. Without them, every lookup would be a slow scan of blocks.

Why do hashmaps matter in blockchain?

A hashmap is a data structure that stores key-value pairs and finds any value from its key in near-constant time, and blockchains use it to hold account balances, contract storage and transaction lookups. When a wallet asks for a balance, the node does not scan every block. It hashes the address, jumps to one bucket in the map, and reads the value.

That speed matters because a node answers thousands of lookups a day. A chain with a million accounts cannot afford a linear scan for each one. A hashmap turns a search that grows with the size of the chain into a search that stays roughly the same no matter how large the chain gets.

Hashmaps also shape how blocks are verified. A block header carries a single hash that commits to the whole state, and the state itself is organised as a key-value map. The node can prove that one account has a given balance without sending the entire state, because the map's structure ties each key to that root hash.

If you are building on a chain, the practical point is simple: your reads are fast because a hashmap sits between the request and the data. Your writes are slower, because changing one value means updating the map and recomputing the hashes above it.

How does a blockchain store state in a hashmap?

A blockchain stores its current state as a key-value map in which the key is derived from an address or storage slot and the value is the account or stored data. Ethereum uses a Merkle-Patricia trie, which is a hashmap organised as a tree so that every key has a path from the root hash down to its value.

In Ethereum, the key for an account is the hash of its address, and the value holds the balance, the nonce, the code hash and the storage root. A smart contract's storage works the same way: each storage slot is a key, and the value is the 32-byte word stored there. Reading a contract variable is a hashmap lookup.

Bitcoin organises state differently but with the same idea. The UTXO set is a map from outpoint to unspent output, so a node can check whether a coin has already been spent by looking up one key. Full nodes keep this map in a fast key-value database such as LevelDB or RocksDB.

The shared pattern is that the chain never keeps its live state as a list to scan. It keeps it as a map keyed by hashes, which is what makes a node able to validate a new block in seconds even after years of history.

What makes a hashmap lookup so fast?

A hashmap lookup is fast because a hash function turns the key into a number that points straight at the bucket holding the value. Instead of comparing the key against every entry, the map computes one hash and checks one small group of entries.

Three properties decide the speed:

  • The hash function spreads keys evenly. Similar addresses should land in different buckets, so no single bucket grows long.
  • Buckets stay short. When the map grows, it resizes and rehashes so that each bucket holds only a few entries.
  • Collisions are rare and cheap. Two keys can share a bucket, but the map only compares keys inside that bucket, not across the whole map.

On average, a lookup costs about the same whether the map holds a thousand entries or ten million. The worst case is a scan of one long bucket, which is why a good hash function and regular resizing matter more than raw speed of any single step.

That average-case speed is what lets a node answer balance and storage queries while it is also validating blocks, gossiping transactions and serving peers.

Where do hashmaps sit inside a node?

Hashmaps sit in several places inside a node, and each one answers a different kind of question. The state database is the largest, but the mempool and the indexes matter just as much for day-to-day operation.

Node component What the map holds Key What it answers
State database Account balances and contract storage Address or slot hash What is this balance right now?
UTXO set Unspent transaction outputs Transaction id and output index Has this coin been spent?
Mempool Pending transactions Transaction hash Is this transaction already waiting?
Block index Block headers and positions on disk Block hash Where is this block stored?
Contract storage Variables of one contract Storage slot What value sits in this slot?
Peer table Known peers and their state Peer id Who can I sync from?

Notice that every row uses a hash as the key. Hashes are fixed-length, evenly spread and easy to compare, which is exactly what a hashmap wants. That is one reason blockchains hash nearly everything before storing it.

For teams building chain software, the blockchain development service page covers the node and indexing choices that follow from this layout.

Example (hypothetical): checking 10,000 balances

Example (hypothetical): a wallet service holds 10,000 customer accounts and needs to show each balance when a user opens the app. The node stores the balances in a hashmap keyed by address hash.

With a linear scan, each lookup would check on average half the list, or 5,000 entries. For 200 balance requests in one minute, that is 200 times 5,000, or 1,000,000 comparisons. With a hashmap, each lookup checks about one bucket, so 200 requests need about 200 comparisons.

The arithmetic shows why the map is not a minor optimisation. One million comparisons against 200 is a factor of 5,000, and the gap grows with the number of accounts. At one million accounts, the scan would average 500,000 checks per lookup while the hashmap still checks about one bucket.

The same logic applies to contract storage. A contract with 10,000 storage slots reads any slot in about one step, which is why a read-heavy contract stays cheap to query even as its state grows.

What are the common mistakes?

The most common mistakes with hashmaps in blockchain work are treating the map as a list, trusting a bad hash function, and forgetting that the map and the proof structure must stay in step.

  • Scanning instead of looking up. Iterating over all accounts to find one balance throws away the speed the map exists to provide.
  • Using a weak hash function. If similar keys cluster in a few buckets, lookups slow down and an attacker can force worst-case behaviour.
  • Forgetting the trie root. In a Merkle-Patricia trie, changing a value means updating every hash on the path to the root. Skipping that step breaks proofs.
  • Confusing the map with the chain. The hashmap holds current state. History lives in blocks, and you cannot reconstruct past balances from the map alone.
  • Storing unbounded data in contract storage. Every slot is a map entry the node must keep, so growing storage without a plan raises costs for everyone running a node.

A related mistake is assuming that a fast lookup means a fast write. Writes must update the map, the trie hashes and the disk, so they cost more than reads. Design read-heavy features around that gap.

How do hashmaps relate to Merkle trees?

Hashmaps and Merkle trees solve different problems, and blockchains use both: the hashmap answers 'what is the value for this key', while the Merkle tree proves 'this value is part of the agreed state'.

Ethereum's Merkle-Patricia trie is the meeting point of the two ideas. It is a hashmap in the sense that you look up a key and get a value, and it is a Merkle tree in the sense that every node carries a hash of its children, so the root commits to the whole map. A light client can verify one balance with a short proof instead of downloading the state.

Bitcoin keeps the two structures separate. The UTXO set is a plain key-value map for fast checks, while each block header carries a Merkle root over the block's transactions. The map is for speed, the tree is for proof.

When you design a chain or a layer-2 system, decide which of the two you need. If you only need fast lookups, a plain hashmap is enough. If other parties must verify a value without trusting you, you need the tree structure that a Merkle-Patricia trie provides. The Ethereum and smart contracts guide shows how the trie works in a live chain.

Key takeaways

  • Hashmaps turn balance and storage lookups into near-constant-time operations
  • Ethereum stores state in a Merkle-Patricia trie, a hashmap that also proves values
  • Bitcoin keeps the UTXO set as a key-value map for fast spent-coin checks
  • A node uses hashmaps for state, the mempool, the block index and peer tables
  • The map holds current state only; history lives in blocks and needs an archive

Frequently asked questions

What is a hashmap in simple terms?

A hashmap stores key-value pairs and finds a value by hashing its key to a bucket, then checking only that bucket. A phone book sorted by name is a rough analogy: you jump to the right letter instead of reading every page. The hash function is what makes the jump possible.

Why does Ethereum use a Merkle-Patricia trie?

The trie is a hashmap organised as a tree, so each key has a path from the root hash to its value. That means a node can prove one account balance with a short proof, without sending the whole state. It combines fast lookup with verifiable proof in one structure.

Do hashmaps make blockchains faster to write to?

No, they mainly speed up reads. A write must update the map, recompute the hashes on the path to the root in a trie, and persist the change to disk. Reads are cheap because they touch one bucket; writes pay for keeping the proof structure correct.

What happens when two keys hash to the same bucket?

That is a collision, and the map stores both entries in the same bucket, then compares keys inside that bucket to find the right one. Collisions are normal and cheap when the hash function spreads keys evenly. They only hurt when many keys cluster in one bucket.

Can a hashmap hold the full history of a chain?

No. A hashmap holds the current state, such as today's balances and contract storage. Past states live in blocks, and a node that prunes old state cannot answer historical queries from the map alone. Archive nodes keep past states for that purpose.

Is a hashmap the same as a database?

No. A hashmap is a data structure inside a program, while a database is a storage system. Nodes usually store their hashmaps in a key-value database such as LevelDB or RocksDB, which persists the map to disk and handles caching, compaction and crash recovery.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building chain software and want the state layer designed right? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Blockchain Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call