STORAGE, CACHING & SEARCH · SUBTOPIC
B-Trees vs. LSM-Trees
Why PostgreSQL uses B-Trees while Cassandra and RocksDB use LSM-Trees.
IntermediatePart of Indexing & Storage Engines
Subtopics in Indexing & Storage Engines (2)
In plain words
A B-Tree keeps an alphabetically sorted filing cabinet and inserts each new file into its exact drawer immediately. An LSM-Tree stacks new papers neatly in an inbox (super fast!) and sorts them into folders later at night.
Think of it like…
Sorting books onto shelves as they arrive (B-Tree) vs. piling returns on a cart and shelving them in batches (LSM-Tree).
Loading interactive diagram
Loading interactive exercise
Theory & how it works
Compaction spikes to watch for
In LSM-Trees, background compaction merges older SSTable files and clears deleted rows ("tombstones"). Under heavy write load, compaction can briefly compete for disk I/O and spike read latency (p99).
Key points & practical guide
Summary checklist
- B-Trees = predictable fast reads, in-place page updates
- LSM-Trees = sequential fast writes, background compaction
- Both use WAL for crash durability
When to use it
- B-Trees for general SQL/OLTP
- LSM-Trees for telemetry, logs, messaging history, write-heavy NoSQL
Watch out for
- Unbounded compaction lag on LSM engines when disk IOPS saturate
Quick check
Loading quiz