QMDB
How QMDB combines key-value storage and Merkle tree maintenance to reduce disk I/O.
QMDB (Quick Merkle Database) is a verifiable database built by Commonware and LayerZero. It combines key-value storage and Merkle tree maintenance to reduce disk I/O during state updates.
QMDB appends new entries rather than overwriting existing ones. It groups entries into subtrees called “twigs,” each with 2,048 leaf entries, and marks superseded entries inactive. This design provides:
- One SSD read per state access: the indexer provides the file offset directly.
- O(1) I/O for updates: new entries go to an in-memory twig, which is written sequentially to SSD when full.
- In-memory Merkle tree maintenance, avoiding disk I/O for hash updates.
For example, updating a token balance follows this sequence:
sequenceDiagram
participant App
participant Indexer as Indexer (RAM)
participant Twig as Fresh Twig (RAM)
participant SSD
App->>Indexer: Where is key K?
Indexer->>SSD: Read at offset 0x3F…
SSD-->>App: Old entry E
App->>Twig: Append new entry E'
Note over Twig: E'.OldId = E.Id
App->>Indexer: K now points to E'
Note over SSD: Old entry stays on disk,
just marked inactive
The in-memory B-tree indexer provides the file offset, so reading the old entry requires one SSD read without traversing the Merkle tree on disk. The new entry is appended to the current twig in RAM. When the twig reaches 2,048 entries, it is written sequentially to disk. Hash updates happen in memory.
The paper contrasts this with traditional designs that store tree nodes and data in separate on-disk key-value stores. It describes O((log N)^2) disk I/O per update for those designs, compared with O(1) for QMDB.
Reported benchmarks
The paper reports these results on an AWS c7gd.metal machine with 64 vCPUs, 256 GB of DRAM, and two NVMe SSDs:
- 6 times the throughput of RocksDB.
- 8 times the throughput of NOMT, another verifiable database.
- 601,000 updates per second at 6 billion entries.
On an i8g.metal-24xl machine, the reported throughput reaches 2.28 million updates per second, supporting over 1 million token transfers per second.
On a $540 mini PC with an AMD R7-5825U, 64 GB of DDR4 RAM, and a 4 TB NVMe drive, the reported throughput is 150,000 updates per second with up to 1 billion entries.
The tests included databases with 15 billion entries, which the paper compares with 10 times Ethereum’s 2024 state size. It gives a theoretical limit of 280 billion entries on a single machine.
Memory efficiency
For 1 billion entries, QMDB needs roughly 160 MB of DRAM for twig roots and bitmaps. The in-memory indexer requires additional memory: about 15.4 bytes per key. A hybrid variant optimized for SSDs reduces the indexer requirement to 2.3 bytes per key.
When all entries in a twig have been superseded, QMDB can prune the subtree to a 32-byte hash and a 256-byte bitmap, reducing its storage by about 99.9%.
graph LR
A["Full Twig\n2,048 entries\n~256 KB"] -- "all entries overwritten\nby newer values" --> B["Pruned Twig\n32-byte hash\n+ 256-byte bitmap"]
style A fill:#E84855,color:#fff
style B fill:#3BB273,color:#fff
Why it matters
Blockchains store state such as account balances, contract storage, and token ownership. Merkle proofs let clients verify entries against a state root, including claims that a key is absent. Faster state updates and proof computation can reduce database bottlenecks in transaction processing.
QMDB also supports historical state queries and exclusion proofs, which prove that a key does not exist.
Each entry stores a NextKey pointer to the lexicographically next key in the database. An exclusion proof uses the entry whose key and NextKey surround the absent key:
graph LR
A["Entry: alice\nNextKey = charlie"] --> B["Entry: charlie\nNextKey = dave"]
B --> C["Entry: dave\nNextKey = ..."]
style A fill:#2A40FF,color:#fff
To prove that bob is absent, provide a Merkle proof for alice’s entry. Its NextKey is charlie, so bob cannot be present between them. This requires one Merkle proof rather than a scan.
Historical queries use an OldId field linking an entry to the previous version of the same key. Following those links lets the database reconstruct earlier state.
What about agents?
I’m interested in using this design for an agent permission graph. Agents executing transactions, delegating work, and managing access need a verifiable record of who can do what.
An exclusion proof could show that a revocation entry or delegation entry is absent without requiring the agent to download the full graph. Interpreting that absence still depends on the protocol’s permission rules. Historical queries could help auditors reconstruct the permissions recorded when an agent acted.
The reported update rates suggest QMDB could support frequent permission changes. A permission-graph implementation would still need its own benchmarks.
Further reading
The QMDB paper describes the design and benchmarks. The source code is MIT/Apache-2 licensed. Commonware’s companion post explains the design decisions and its place in their infrastructure work.