B-Trees vs LSM-Trees
How databases lay out and index bytes on SSD/NVMe disks for read-heavy vs. write-heavy workloads
Difficulty: Advanced · Read time: 9 min · Access: Step-1 Preview + Knoera Pro
How It Works
B-Trees (Balanced Trees, used by PostgreSQL and MySQL InnoDB) update fixed 4 KB–16 KB pages in-place on disk, delivering super predictable O(log N) reads. LSM-Trees (Log-Structured Merge-Trees, used by Apache Cassandra, RocksDB, and LevelDB) never overwrite data in-place: they append every write sequentially to a disk Write-Ahead Log (WAL) plus an in-memory MemTable, then flush sorted immutable SSTables (Sorted String Tables) to disk and merge them via background compaction.
Why It Matters
Random 4 KB–16 KB disk overwrites and page splits in B-Trees bottleneck extreme write-heavy ingestion (like metrics, logs, or chat streams), whereas LSM-Trees can suffer from severe read amplification and SSD (Solid-State Drive) I/O (Input/Output) compaction stalls if Bloom filters and compaction budgets are misconfigured.
Interactive Architecture Scenarios
- LSM-Tree Write & SSTable Compaction
- Failure Mode: Read Amplification & Compaction Storm