Storage Engines: B-Tree vs. LSM-Tree

Difficulty
Importance

The problem

A database's index has to live on disk, not just in memory, and disk access is orders of magnitude slower than RAM — the data structure chosen to organize that on-disk data determines whether the database is optimized for fast reads, fast writes, or some balance between the two, and different workloads genuinely need different answers.

Why now

Indexes and query performance already established that B-trees back most relational indexes, treating the tree as a given; this topic opens that up, explaining why B-trees were chosen (in-place updates, good read performance) and introducing the LSM-tree as a fundamentally different, write-optimized alternative most NoSQL and time-series databases actually use instead.

Mental model

A B-tree keeps data sorted on disk and updates it in place — great for reads (one predictable path down the tree), but every write risks an expensive random disk seek to the right page. An LSM-tree instead never updates in place: writes go to an in-memory buffer that's periodically flushed to disk as an immutable sorted file, with older files merged (compacted) in the background — trading slightly slower, multi-file reads for dramatically faster, sequential-only writes.

Requires

Unlocks

Used in

Projects

  • Diagram how a B-tree handles an insert (in-place page update, possible page split) versus how an LSM-tree handles the same insert (memtable write, eventual flush and compaction)
  • Benchmark write throughput on a B-tree-based database versus an LSM-tree-based one under a heavy sequential-write workload, and explain the result in terms of disk seek behavior

Examples

  • A B-tree insert that causes a page to overflow triggers a page split, which can cascade up the tree — a real, bounded cost paid at write time in exchange for consistently fast reads
  • An LSM-tree's compaction process merging several sorted files in the background is directly analogous to merge sort's merge step — the same algorithm, running continuously on disk

Resources

Mastery checklist