System Designcalculator

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.

LSM-tree needs fewer I/Os

At 50% reads, the B-tree needs 5.5 page I/Os per operation and the leveled LSM-tree needs 1.03.

I/Os per operation at 50% readsLSM-tree: 5.4× fewerB-tree 5.5 · LSM-tree 1.03
0×10×20×30×40×102030Size ratio (T)Write amplificationT = 10
LSM-tree, leveledLSM-tree, tieredB-tree

Inputs

116 GB of keys and values.

Page size
Compaction style

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.

B-tree and LSM-tree costs side by side. Lower is better in every row except depth.
CostB-treeLSM-tree (leveled)Lower
DepthB-tree pages root to leaf; LSM-tree levels and sorted runs a lookup may probe5 pages4 levels, 4 runs–
Write amplificationBytes written per byte stored, without the log35.3×18×LSM-tree
Page I/Os per writeAveraged over many writes; includes compaction reads61.03LSM-tree
Point read, key exists, with filtersBloom filters at 10 bits per key; a B-tree has none51.02LSM-tree
Point read, key exists, no filters53.89LSM-tree
Point read, key missing, with filters50.03LSM-tree
Point read, key missing, no filters54LSM-tree
Range read, 1,000 entriesPage I/Os: one seek per run, then the pages scanned4633LSM-tree
Space amplificationSize on disk per byte of live data; LSM-tree is the worst case1.48×1.11×LSM-tree

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

Sources

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.