skip to content
jaeaeichsysdes

B-Trees vs LSM-Trees

Deriving the two dominant database storage engines from disk physics: in-place random page updates versus append-only sorted runs and compaction.

Every database eventually hits the same physical constraint: disks reward sequential I/O and punish random I/O. Even on modern NVMe SSDs, overwriting small scattered regions triggers internal flash erase-block amplification, while streaming large sequential blocks saturates bandwidth cleanly.

Storage engines diverge on a single question: when a write arrives, do you update the sorted structure on disk right now, or append the change and sort it later?

Engine 1: B-Trees (update in place)

A B-Tree organizes data into fixed-size pages (typically 8 KB in PostgreSQL, 16 KB in MySQL InnoDB). Internal pages hold routing keys; leaf pages hold the actual rows or pointers in sorted order.

  • Reads are predictable: a 4-level B-Tree with 500 keys per page indexes over 60 billion rows in at most 4 page lookups (and the top 2–3 levels stay pinned in RAM).
  • Writes mutate pages in place: updating a key requires locating its leaf page, modifying it in the buffer pool, logging the change to a WAL for crash durability, and eventually flushing the dirty 8–16 KB page back to disk.
  • Hidden cost: when a leaf page runs out of space, it triggers a page split, writing two half-full pages plus updating the parent. Modifying a 100-byte row can force rewriting an entire 16 KB page (high write amplification).

Engine 2: LSM-Trees (append now, merge later)

Log-Structured Merge-Trees (RocksDB, Cassandra, ScyllaDB) flip the priority: never seek on disk during a write.

  1. Append to the WAL: sequential disk append for crash durability.
  2. Insert into the MemTable: an in-memory balanced tree (red-black tree or skiplist) keeps recent writes sorted in RAM.
  3. Flush to an L0 SSTable: when the MemTable hits its size limit, freeze it and flush it sequentially to disk as an immutable, sorted file.
  4. Background compaction: periodically merge multiple SSTables into larger, non-overlapping levels (L1, L2…), dropping overwritten values and delete tombstones.

O(1)

LSM write path · sequential append

O(log N)

B-Tree point read · 3–4 pages

1–2%

Bloom filter false positive rate

Interactive lab: watch MemTable flushes and compaction

Write keys, overwrite the hot key u:42, insert a tombstone delete, and run compaction below. Watch how LSM avoids random disk seeks while accumulating L0 files until compaction collapses duplicates and tombstones.

interactive · fill the MemTable, accumulate L0 SSTables, then run compaction

lsm-tree (append-only)6 seq writes · 2x read amp
memtable
u:19=v1u:42=v2··
l0 sstable
L0-1u:04=v1 · u:08=v1 · u:15=v1 · u:42=v1
l1 merged
empty — run compaction to merge L0 files
b-tree (in-place pages)6 random seeks · 3x read amp
active keysu:04 · u:08 · u:15 · u:19 · u:42
hot key u:42v2 (updated in place)
wal tailSET u:42=v2

L0 files: 1 · LSM read checks: 2L0-1 flushed; u:42 now exists in both MemTable (v2) and L0-1 (v1)

Making LSM reads fast: Bloom filters and sparse indexes

Without help, a point lookup for a missing key would have to check the MemTable and every L0 SSTable. Production LSM engines attach two structures to each SSTable:

  • Bloom filter: a compact bit array in RAM that answers “definitely not in this file” in nanoseconds, skipping 98–99% of unnecessary disk reads (with a tiny false positive rate).
  • Sparse key index: stores the byte offset of every Nth key so a lookup binary-searches in RAM and performs one small sequential read on disk.

The RUM Conjecture

Storage researchers summarize the design space with the RUM Conjecture: a data structure can optimize at most two of Read overhead, Update (write) overhead, and Memory (space) overhead.

Dimension B-Tree (Postgres / InnoDB) LSM-Tree (RocksDB / Cassandra)
Write path Random in-place page overwrites + WAL Sequential WAL + MemTable + sequential flush
Read path Deterministic 3–4 page lookups Check MemTable + L0 files + L1..Lk (helped by Bloom filters)
Space efficiency Page fragmentation (~60–75% fill on random keys) High compression; temporary space spike during compaction
Tail latency risk Checkpoint write bursts & page splits Compaction I/O storms & write stalls if L0 backs up

note

Deletes in an LSM-Tree do not free disk space immediately. A delete appends a tombstone record that shadows older versions until compaction merges the tombstone all the way to the bottom level. Too many uncompacted tombstones can slow range scans dramatically.

Where this shows up in system design

  • PostgreSQL & MySQL (OLTP)B-Tree primary and secondary indexes deliver predictable single-row lookups, range queries, and strict transactional isolation.
  • Cassandra / ScyllaDB / RocksDBLSM-Trees absorb millions of writes/sec for high-ingest telemetry, messaging, and wide-column event stores.
  • Discord Message StorageMigrated from MongoDB to Cassandra and then ScyllaDB (LSM-backed) to sustain billions of append-heavy chat messages without B-Tree page thrashing.
  • Time-Series Databases (InfluxDB / Prometheus)Append-only time-partitioned LSM blocks compress metrics 10x better than in-place B-Tree pages.

Check yourself: system design scenarios

interactive · choose the right storage engine for each workload

1. IoT fleet telemetry ingestion

500k writes/s · 50:1 write/read · high compression

500,000 vehicles emit GPS and engine sensor readings every second. Writes outnumber reads 50:1, and queries mostly scan recent time ranges for a single vehicle.

2. Core banking ledger with strict p99 read latency

10:1 read/write · strict 5ms p99 · point lookups

An account balance service handles 10x more point reads than writes. Every balance check must finish under 5ms p99 with zero background compaction stalls.

3. Diagnosing slow range scans after bulk deletes

LSM-Tree · 80% deleted · slow range scans

Your Cassandra cluster (LSM-Tree) deleted 80% of old rows from a hot partition yesterday. Today, range queries over that partition time out even though few live rows remain.

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme