B-tree and LSM costs
A B-tree updates data in place: to change one entry it finds the leaf page and rewrites the whole page. An LSM-tree never rewrites in place. It buffers writes in memory, flushes them as sorted files, and merges those files in the background, which is called compaction.
At 50% reads, the B-tree needs 5.5 page I/Os per operation and the leveled LSM-tree needs 1.03.
Inputs
116 GB of keys and values.
Each level holds T times more than the one above.
0 turns filters off.
An assumption; it sets how many levels the data needs.
The rest are writes. Reads are point reads of keys that exist.
| Cost | B-tree | LSM-tree (leveled) | Lower |
|---|---|---|---|
| DepthB-tree pages root to leaf; LSM-tree levels and sorted runs a lookup may probe | 5 pages | 4 levels, 4 runs | – |
| Write amplificationBytes written per byte stored, without the log | 35.3× | 18× | LSM-tree |
| Page I/Os per writeAveraged over many writes; includes compaction reads | 6 | 1.03 | LSM-tree |
| Point read, key exists, with filtersBloom filters at 10 bits per key; a B-tree has none | 5 | 1.02 | LSM-tree |
| Point read, key exists, no filters | 5 | 3.89 | LSM-tree |
| Point read, key missing, with filters | 5 | 0.03 | LSM-tree |
| Point read, key missing, no filters | 5 | 4 | LSM-tree |
| Range read, 1,000 entriesPage I/Os: one seek per run, then the pages scanned | 46 | 33 | LSM-tree |
| Space amplificationSize on disk per byte of live data; LSM-tree is the worst case | 1.48× | 1.11× | LSM-tree |
Every number is a page-I/O count from closed-form formulas, with no cache, no compression and no concurrent work. Use it to see which way each knob pushes, not to predict your latency.
- Every page access is one I/O of equal cost. Nothing is cached, and a seek costs the same as a sequential read.
- B-tree pages are 69% full on average, which is typical after random inserts, and a child pointer takes 8 bytes. Both are assumptions.
- A B-tree write reads the leaf, then writes it back; its write amplification is the page size divided by the entry size.
- LSM levels are full, a page write costs the same as a page read, and every run uses the same Bloom filter bits per key, as RocksDB does by default. Monkey shows that giving deeper levels fewer bits does better.
- A tiered level holds up to T − 1 runs. Space amplification is the worst case where every update overwrites a live key.
- Write amplification leaves out the write-ahead log, which adds about 1× to both.
What it shows
The two designs pay in different places. The B-tree writes more bytes per update but finds a key along one short path. The LSM-tree writes fewer random pages but may check several files to find a key, so it adds Bloom filters to skip most of them.
Set your data and workload, and the tool counts page I/Os for both. It is a cost model, not a benchmark. It ignores caches, compression, the write-ahead log and the speed gap between random and sequential I/O.
How to read it
- The headline compares I/Os per operation at the share of reads you set. Reads are point reads of keys that exist.
- In the table, “Lower” names the engine with the smaller number in each row. Depth has no winner.
- Size ratio (T) is how much bigger each LSM level is than the one above. Higher T means fewer levels and cheaper reads, but leveled compaction rewrites data more often.
- Leveled keeps one sorted run per level: cheaper reads, less space, more write amplification. Tiered keeps up to T − 1 runs: cheaper writes, more runs to check, more space.
- Point reads cover a key that exists and one that is missing, with and without filters. A B-tree has no filters, so its cost is the same in all four.
- The chart plots LSM write amplification against T. The dashed line is the B-tree; the marker is your T.
Questions
Is an LSM-tree always faster for writes?
It does fewer page I/Os per write in this model, because it batches writes and merges sorted files sequentially. With leveled compaction and a large T, though, each byte is rewritten many times and write amplification can pass the B-tree’s. Tiered compaction trades that back for reads.
Why does the LSM-tree need Bloom filters?
A key may sit in any run, and a missing key has to be ruled out in all of them. A Bloom filter per run says “definitely not here” for most runs without touching the disk, so a lookup costs about one I/O instead of one per run.
What do the three overheads in the RUM Conjecture mean?
Read, update and memory (space) overhead. The RUM paper argues that an access method can be tuned to lower two of them only by raising the third. The two compaction styles here are two points on that trade-off.
How accurate are these numbers?
They are page-I/O counts from published formulas, not measurements. Caches keep a B-tree’s upper levels in memory, compression shrinks both designs, and sequential I/O is cheaper than random I/O. Measure your own workload before choosing.
Learn it properly
- System DesignB-trees: the data structure behind most indexes
- System DesignLSM trees: storage designed for fast writes
- System DesignThe LSM write path: memtable, WAL and SSTables
- System DesignThe LSM read path: Bloom filters and sparse indexes
- System DesignCompaction: merging files and reducing amplification
- System DesignBloom filters: quickly checking whether an item exists
Sources
Last updated
More tools
All tools- estimatorBack-of-envelope estimatorTurn daily users into requests per second, storage and bandwidth, with the working shown.Open tool
- simulatorConsistent hashing simulatorA hash ring with virtual nodes: key spread per node, and how many keys move when the cluster changes.Open tool
- estimatorQueue latency estimatorWhy latency explodes as servers get busy: utilisation, waiting time and Little’s law.Open tool