LSM-Trees vs B-Trees (Write Amplification)
Pick a Storage Engine by Its Amplification
Databasesdatabases / storage engines
Every durable database answers one question: where does a write physically land? Two answers dominate, and they trade the same three resources in opposite directions.
B-tree (Postgres, MySQL/InnoDB, SQL Server)
Update in place. Find the 8KB page holding the row, modify it, journal it to the WAL, flush later.
- Write path: random I/O, read-modify-write
- A 200-byte UPDATE can dirty a whole 8KB page → write amplification often 10–40x
- Reads: one tree descent, ~3–4 page lookups. Boringly predictable.
LSM-tree (RocksDB, Cassandra, ScyllaDB, LevelDB)
Never update in place. Writes hit an in-memory memtable + a sequential append log. Memtable fills → flushed as an immutable sorted file (SSTable). Background compaction merges levels.
- Write path: sequential only. Enormous ingest throughput.
- Reads: a key may be in the memtable or ANY level → several files consulted (bloom filters prune most)
- DELETE writes a tombstone; the bytes stay on disk until compaction
Side by side (B-tree → LSM)
- Write pattern: random → sequential
- Write amp: 10–40x → 5–15x (leveled)
- Read amp: ~1 → one lookup per level
- Space amp: ~1.3x page bloat → ~1.1x leveled, 2x+ tiered
- Range scans: great → good (merge of sorted runs)
- Latency shape: flat → spiky, compaction stalls
The gotcha that trips people
"LSM = faster writes" is half a sentence. The work is deferred, not avoided. Compaction runs later, competing with your reads for the same disk and CPU.
Benchmark 60 seconds and LSM looks magical. Run 6 hours at steady state, past the point where the dataset exceeds RAM, and you meet write stalls: p50 still fine, p99 100x worse, because a compaction backlog tripped the stop-writes threshold (RocksDB: level0_stop_writes_trigger).
So the honest metric is never ops/sec on a fresh database. It's amplification at steady state.
Choosing
- Read-heavy, point lookups + joins, real transactions → B-tree.
- Write-heavy ingest: events, metrics, logs. Keys written once, scanned by recent time → LSM.
- Heavy deletes or repeated overwrites of the SAME key → LSM punishes you. Tombstones pile up, space amp climbs, and range scans have to read and discard them. The classic trap is a job-queue table on an LSM engine: you overwrite and delete every row, so you pay compaction forever for data you no longer want.
The nuance that makes it click
Both engines are the same bet made in opposite directions: B-trees pay cost up front per write to keep reads cheap; LSMs take the write cheaply and pay in background work plus read fan-out. Neither "wins" — you're choosing which resource you have spare.
Your exercise
On Postgres, insert 1M rows keyed by random UUIDv4, then 1M keyed by UUIDv7, same table shape. Compare wal_bytes from pg_stat_wal before/after each run.
The gap you measure IS write amplification from randomly dirtying pages all over the tree. Then answer this in one sentence: why would the same experiment on RocksDB show almost no difference between the two key types?