Database internals / Intermediate / 5 min / Updated
B-tree vs LSM tree: how databases store rows on disk
Slide the write share and compare a B-tree with an LSM tree: random page writes vs sequential files, compaction levels and Bloom filters.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
What is B-tree vs LSM?
A B-tree updates sorted pages in place; an LSM tree buffers writes in memory and merges sorted files in the background.
Why does it matter?
The storage engine decides how fast a database writes and reads, and how much the disk wears.
What is the trade-off?
B-trees favour reads and steady latency; LSM trees favour writes but add compaction work and multi-level reads.
How do you explain it in an interview?
"Write-heavy time series in an LSM store like Cassandra or RocksDB; read-heavy catalogue in Postgres on B-trees."
Check yourself
Why are LSM trees good at heavy write loads?
Answer: Writes go to memory and are flushed as sorted files in one sequential write. Sequential writes are far cheaper than rewriting random pages; compaction merges files later.
What do Bloom filters do in an LSM tree?
Answer: Tell a read which levels cannot contain the key, so it skips them. A small in-memory filter answers "definitely not here" for most files, so a lookup usually reads one file.
Why does a B-tree suit read-heavy workloads?
Answer: Each key lives in exactly one page, so a read is one page with the upper levels cached. Updates in place keep one copy of each key; LSM reads may need to check several levels.