Notes

Part 6 of 10

Comparing B-trees and LSM-trees

The rough rule: LSM-trees suit write-heavy apps, and B-trees are faster for reads. These are only rules of thumb. Benchmarks swing a lot with small workload details, so test with your own data and queries. The two are not strict rivals either, since some engines mix both ideas, such as several B-trees merged in LSM style.

Read performance

  • A B-tree lookup reads one page per level, and there are few levels, so reads are fast and predictable.
  • An LSM lookup may need to check several files.
  • Bloom filters cut most of that disk work, so the winner varies by engine and by the kind of load.
  • On a B-tree, a range query just walks the sorted leaves.
  • On an LSM-tree, a range query must scan every segment in parallel and merge the results.
  • Bloom filters cannot help range queries, because you cannot hash every possible key in a range, so LSM range queries cost more than single-key lookups.
  • Under very heavy writes, the memtable can fill up faster than flushing and compaction can keep up.
  • Engines like RocksDB then apply backpressure: they pause all reads and writes until the memtable is flushed, which shows up as latency spikes.

Sequential versus random writes

  • In a B-tree, writes to keys spread across the key space land on pages all over the disk.
  • These are random writes: many small, scattered writes.
  • An LSM-tree writes whole segment files at once, during flushes and compaction, and those files are much bigger than a page.
  • These are sequential writes: fewer, much larger writes.
  • Disks handle sequential writes faster than random ones, so an LSM-tree usually sustains more writes on the same hardware.
  • The gap is huge on spinning disks and smaller, but still real, on SSDs.

On a spinning disk, each random write must move the head and wait for the platter to turn to the right spot, which takes several milliseconds. SSDs have no moving parts, but they still prefer sequential writes:

  • Flash is written in small pages of about 4 KiB but can only be erased in large blocks of about 512 KiB.
  • Before erasing a block, the drive must copy any still-valid pages somewhere else.
  • This copying is SSD garbage collection.
  • Sequential writes tend to fill a whole block with one file, so when the file is deleted the whole block is free with no copying.
  • Random writes leave blocks with a mix of live and dead pages, so garbage collection copies more and eats into write bandwidth.
  • Those extra writes also wear out the flash, so random writes shorten a drive's life.

Write amplification

Every engine turns one write from your app into several writes on disk.

  • LSM-tree: once to the crash log, again when the memtable is flushed, and again in each compaction that includes the value.
  • B-tree: at least twice, once to the WAL and once to the page.
  • A B-tree sometimes rewrites a whole page to change a few bytes, so the page survives a crash or power cut.

Write amplification is the bytes written to disk divided by the bytes that a plain append-only log with no index would write. Some people count I/O operations instead of bytes. In write-heavy apps, disk bandwidth is often the limit, so higher write amplification means fewer writes per second. Both engine types suffer from it. For typical workloads LSM-trees usually have lower write amplification, since they skip full-page rewrites and compress their files. The real numbers depend on key and value sizes and on how often keys are overwritten versus newly inserted. Lower write amplification also means less SSD wear.

A benchmark trap: a fresh, empty LSM-tree has not started compacting yet. Run tests long enough for compaction to compete with your writes for disk bandwidth.

Try it

Same writes, two engines

0 keys written by the app

B-tree

Pages on disk, and how often each was rewritten

  1. p10
  2. p20
  3. p30
  4. p40
  5. p50
  6. p60
  7. p70
  8. p80

LSM-tree

Memtable (0 of 4)

  1. -
  2. -
  3. -
  4. -

Segments on disk, newest first

  1. None yet
MeasureB-treeLSM
Bytes written to disk0 KB0 KB
Write amplification0×0×
Scattered writes00
In-order writes00

Nothing written yet.

A toy model: 100-byte records, 4 KB pages, and no caching, so every B-tree write rewrites a whole page. Real engines keep busy pages in memory and the numbers vary a lot, so measure with your own data.

Disk space usage

  • B-trees fragment: after many deletes, empty pages sit in the middle of the file.
  • The tree can reuse them, but handing the space back to the operating system needs a background process that moves pages around.
  • LSM-trees fragment less: compaction rewrites files anyway, and segments have no half-empty pages.
  • LSM blocks also compress well, so the same data often takes less disk.
  • Old values still take space in an LSM-tree until compaction removes them.
  • With leveled compaction that extra is small, while size-tiered uses more, especially during big merges.
  • Deletes can linger: a deleted record may survive in older levels until its tombstone has passed through all of them.
  • That can take a long time, which matters for privacy laws that require data to really be gone.
  • Snapshots are cheap in an LSM-tree: flush the memtable, note which files exist, and keep those files from being deleted, with no copying needed.
  • In a B-tree that overwrites pages in place, snapshots are harder.