Under every database is a storage engine, and almost all of them make one of two bets about how to put data on disk — a choice that quietly decides whether the system is fast at writes or fast at reads. A B-tree (Postgres, MySQL/InnoDB, most classic databases) keeps data sorted in fixed pages and updates them in place: a lookup is a short, predictable walk down the tree, so reads are cheap and stable — but every write has to find and rewrite a page wherever it lives, which on disk means a random write. An LSM-tree (RocksDB, Cassandra, LevelDB, most write-heavy stores) makes the opposite bet: it appends every write to a small in-memory buffer, and when that fills it flushes the buffer to disk as one sorted run — a single sequential write — then merges runs together in the background (compaction). Writes become cheap and sequential, but a read may have to check the buffer and several runs to find the newest value (read amplification, tamed by bloom filters). Stream the same writes into both engines and watch the trade-off happen in real time.
Sorted pages updated in place. Reads walk down the tree (cheap, stable); writes rewrite a page wherever it lives (random I/O).
LSM-tree
Writes append to an in-memory memtable, flushed to disk as sorted runs, merged by background compaction.
write amplification
Compaction rewrites data multiple times as runs merge — the cost LSM pays for cheap incoming writes.
read amplification
A read may check the memtable plus several runs to find the newest value. Bloom filters cut the misses.
engine.js — random writes vs append & merge
Ready
Send the same writes into a B-tree (updates in place) and an LSM-tree (appends, flushes, compacts). Watch the B-tree do a random disk write every time, while the LSM buffers in memory and flushes in batches. Then read a key and compare the cost.
0
writes sent
0
B-tree random writes
0
LSM disk writes
How it works
The two engines are mirror images because they optimise opposite sides of the same tension: disks (and SSDs) love sequential writes and hate scattered random ones. A B-tree keeps one authoritative, sorted copy of the data in pages, so a read is a clean O(log n) descent and always touches roughly the same small number of pages — beautifully stable. The price is on writes: to change a key you must locate its page and rewrite it wherever it already sits, which is a random I/O, and high write rates turn into a storm of scattered page writes. An LSM-tree refuses to do random writes at all. Incoming writes land in a sorted in-memory memtable; when it fills, the whole thing is flushed to disk in one shot as an immutable sorted run — pure sequential I/O — so ingesting writes is extremely cheap. The catch is that the data for one key can now live in the memtable or in any of several runs (with newer runs shadowing older ones), so a read must consult them newest-first until it finds the key: that’s read amplification, which LSM engines fight with per-run bloom filters (skip a run that definitely lacks the key) and with compaction — background merging of runs into fewer, larger ones to keep the number a read must check bounded. Compaction is the hidden cost: it rewrites the same data several times over its lifetime (write amplification). So the rule of thumb is simple — write-heavy workloads favour LSM; read-heavy or range-scan-heavy workloads favour B-trees — and real engines tune where they sit on that spectrum.
1
A write arrives
The B-tree finds the key’s page and rewrites it in place — a random disk write. The LSM-tree just appends the write to its in-memory memtable — no disk I/O at all yet.
2
The memtable fills → flush
When the LSM memtable is full, it is flushed to disk as one immutable sorted run: a single sequential write for a whole batch of updates. The B-tree, meanwhile, has already paid a random write per update.
3
Runs pile up → compaction
Flushed runs accumulate, and a read must check several of them. Background compaction merges runs into fewer, larger ones — rewriting data (write amplification) to keep read cost bounded.
✓
Read the key — count the cost
A B-tree read is a short, stable descent. An LSM read checks the memtable plus each run newest-first (read amplification), skipping runs via bloom filters. Write-heavy? LSM wins. Read/scan-heavy? B-tree wins.
B-tree write
random, in place
LSM write
append → sequential flush
B-tree read
stable O(log n)
LSM read
memtable + runs (amp)
The code
# LSM-tree write path (the write-optimised bet)defput(key, val):
memtable[key] = val # in-memory, ~freeif len(memtable) >= MEMTABLE_CAP:
run = sorted(memtable.items())
flush_sequentially(run) # ONE sequential write for the batch
runs.append(run); memtable.clear()
if len(runs) >= COMPACT_THRESHOLD:
merge(runs) # compaction: rewrite → write amplificationdefget(key): # read amplificationif key in memtable: return memtable[key]
for run in reversed(runs): # newest first; bloom filter skips missesif bloom[run].maybe(key) and key in run: return run[key]
The picture: one write path, one read path
The same key, two layouts. The B-tree has one copy of key 42, so a read always walks the same 3 pages. A write has to go back to that exact leaf, wherever it sits on disk. The LSM never goes back. It writes the newest version to the memtable and, later, into a new run, so a key can have stale copies in older runs. A read therefore checks newest to oldest and stops at the first hit. Bloom filters let it skip run 3 without touching disk, and compaction keeps the number of runs small.
Worked example: 30 writes through the lab’s model
Press Write 30 times. The lab flushes the memtable every 6 writes and compacts when 4 runs pile up. Here is the state at each flush, with the read cost the Read a key button would report.
After write
B-tree random writes
LSM runs on disk (keys)
LSM disk writes
Read cost: B-tree vs LSM
1
1
none (1 key in memtable)
0
3 vs 1
6
6
[6]
1 flush
3 vs 2
12
12
[6, 6]
2
3 vs 3
18
18
[6, 6, 6]
3
3 vs 4: LSM now dearer
24
24
flush makes 4 runs, compaction leaves [24]
5 (flush + merge)
3 vs 2
30
30
[24, 6]
6
3 vs 3
Count the work in keys, not operations. The LSM made 6 disk writes against the B-tree’s 30, but those 6 carried 54 keys: 30 written once by flushes, plus 24 rewritten by the compaction at write 24. That is a write amplification of 54 ÷ 30 = 1.8 in this toy, and it grows with every compaction a key goes through. The B-tree has its own amplification, which is easy to miss. Say each record is 100 bytes on an 8 KB page. If each update’s page were written out on its own, 30 updates would write 30 × 8,192 = 245,760 bytes to store 3,000 bytes of data, about 82 times as much. The difference is the access pattern: the LSM’s bytes are sequential, the B-tree’s are scattered.
Common mistakes
Benchmarking before compaction starts
A short write test that only fills memtables and a few runs measures the cheap half of an LSM. Compaction debt arrives later, and when compaction falls behind, engines slow or stall incoming writes on purpose. Run long enough to reach steady state, then measure.
Thinking a B-tree write waits on a random write
Real B-tree engines append the change to a write-ahead log first (Postgres’s WAL, InnoDB’s redo log). That append is sequential, and the dirty page is written back later at a checkpoint. The random I/O is deferred and batched, not avoided, and it still dominates under heavy write load.
Expecting bloom filters to help range scans
A bloom filter answers one question: “could key k be in this run?” A scan over k1..k2 has to merge the matching slice of every run that overlaps the range. Range-heavy reads are where a B-tree’s single sorted copy wins most clearly.
Forgetting that deletes are writes
An LSM cannot erase a key in an old run, so a delete writes a tombstone that shadows it. The space comes back only when compaction drops both. A workload that deletes a lot can leave reads stepping over piles of tombstones.
Reading the flush counter as the write cost
The lab’s “LSM disk writes” meter counts operations. The cost that wears out SSDs and uses up disk bandwidth is bytes, and compaction rewrites the same bytes again and again, as the 54 keys in the example above show.
Where this shows up in production and interviews
LSMs under other systems
RocksDB is embedded under many systems you may not think of as LSMs. Kafka Streams keeps local state stores in it by default, and TiKV stores its data in it. CockroachDB uses Pebble, an LSM engine written in Go. Picking one of these systems means picking the LSM side of this trade.
Compaction strategy is a tuning knob
Cassandra lets you choose size-tiered compaction (merge runs of similar size: less rewriting, more runs to read and more space held) or leveled compaction (keep each level non-overlapping: more rewriting, fewer runs per read). RocksDB defaults to leveled. It is the lab’s COMPACT threshold, made into a policy.
B-trees behind relational databases
Postgres indexes and InnoDB tables are B+trees: values in the leaves, leaves linked in key order, so a range scan reads neighbouring pages. Their usual pain points are the B-tree side of this lab: random page writes under heavy inserts and pages that split and fragment.
The interview version
For “design a metrics store” or “design chat message storage”, state the read/write mix first, then pick the engine from it. Name the costs you are accepting: read, write and space amplification. You can usually improve two of them at the expense of the third, an idea known as the RUM conjecture. Bonus points for saying that bloom filters fix point reads, not scans.
Quick check
1. Why are writes cheap on an LSM-tree but relatively expensive on a B-tree?
An LSM buffers writes in an in-memory memtable and flushes a whole batch as one sequential run — sequential I/O is cheap. A B-tree must find each key’s page and rewrite it wherever it lives, which is a random write per update. That’s why write-heavy workloads favour LSM.
2. What is read amplification in an LSM-tree?
Because a key’s data can live in the memtable or any of several runs (newer runs shadow older ones), a read consults them newest-first until it finds the key. Checking multiple places for one key is read amplification — mitigated by bloom filters (skip runs that definitely lack the key) and by compaction (fewer runs).
3. What is the purpose of compaction, and what does it cost?
Compaction is the background merging of accumulated runs into fewer, larger sorted files, which keeps the number of runs a read must check bounded. The cost is write amplification: the same data is rewritten several times over its lifetime as runs merge — the hidden price LSM pays for cheap incoming writes.
FAQ
What is the difference between a B-tree and an LSM-tree?
Two storage-engine designs with opposite trade-offs. A B-tree keeps sorted pages updated in place: cheap stable reads, but each write is a random disk write. An LSM-tree appends writes to memory, flushes sorted runs, and compacts in the background: cheap sequential writes, but reads may check several runs (read amplification) and compaction rewrites data (write amplification). B-trees suit read/scan-heavy workloads; LSM-trees suit write-heavy ones.
Which databases use LSM-trees and which use B-trees?
B-tree engines: PostgreSQL, MySQL/InnoDB, most traditional relational databases, and SQLite — where stable reads and range scans matter. LSM engines: RocksDB and LevelDB, Cassandra, ScyllaDB, HBase, and many time-series/write-heavy stores. Some systems let you choose (MySQL can run InnoDB or the RocksDB-based MyRocks), since the right choice depends on the workload’s read/write mix.
What are write amplification and read amplification?
The two hidden costs of an LSM-tree. Write amplification is bytes physically written versus bytes the app asked to write — compaction rewrites data several times as runs merge. Read amplification is how many places a read must check — the memtable plus each run that might hold the key. Bloom filters cut read amplification (skip runs missing the key) and compaction cuts it (fewer runs), but aggressive compaction raises write amplification — so the two are traded off.
When should I choose an LSM-tree over a B-tree?
Favour an LSM-tree for write- or ingest-heavy workloads — logging, metrics, event streams, time-series, anything absorbing a firehose of writes — since appending and sequential flushing keep write cost low. Favour a B-tree for read-heavy, point-read-latency-sensitive, or range-scan/ordered-iteration workloads, where its stable in-place structure gives predictable reads. Mixed workloads are why some engines are tunable and why LSM engines lean on bloom filters and compaction.