Part 3 · 1 chapters · ~8 min

B-Trees and LSM Trees, Side by Side

Why databases need disk-friendly trees, B+trees with high fanout and their page splits (MySQL and Postgres courses), LSM trees and compaction (NoSQL course), read, write and space amplification compared, where each wins, and engines that blend them.

4

Two families of storage engine

B+treeLSM tree
write pathupdate the page in place (via WAL and buffer pool)append to log + memtable; flush sorted files
read pathroot to leaf: about 3-4 page reads for billions of rowsmemtable, then SSTables newest first, Bloom filters to skip
write amplificationpage writes per small change (plus full-page images)rewritten during compaction (often 10× or more with leveled)
read amplificationlowhigher, reduced by Bloom filters and compaction
spacefragmentation from splits and deletesold versions until compacted
used byInnoDB, Postgres, SQLite, WiredTiger (default), LMDBRocksDB, Cassandra, ScyllaDB, LevelDB, Pebble, Kafka Streams stores
best forread-heavy, point and range queries, transactionswrite-heavy, time series, large ingest
code
B+tree height: with 16 KB pages holding ~500 keys per internal node,
  height 3 → 500^3 = 125 million leaf pointers; height 4 → 62.5 billion. Every lookup is ≤ 4 page reads, mostly cached.