Back to questions
MiddleTheoryCommon
How do B-tree and LSM-tree storage engines differ on writes, point reads and range scans?
A B-tree updates pages in place: random writes, one root-to-leaf path per lookup, ordered leaves for ranges. An LSM appends to WAL and memtable, flushes sorted files, compacts them: cheap writes, but reads probe several files; Bloom filters aid point reads.
- Repeating that B-trees always win reads and LSM trees always win writes without naming the read and compaction costs
- Believing a Bloom filter speeds up range scans, when it only answers whether a file may hold a given key
- Forgetting that an LSM delete is a tombstone that lingers until compaction removes it
- How do leveled and tiered compaction trade write, read and space amplification?
- Why is ClickHouse's table engine MergeTree, with immutable parts and background merges, called LSM-like?
Who uses which. B-tree: PostgreSQL, InnoDB in MySQL. LSM: RocksDB, Cassandra. ClickHouse's MergeTree is LSM-like: every insert writes an immutable part, and background merges combine parts.
Why "B-trees win reads, LSM wins writes" is not a law.
- An LSM write is cheap at insert time, but compaction rewrites the same data again and again (write amplification); when memtable flushes or compaction fall behind, the engine slows writes down (a write stall).
- An LSM point read with Bloom filters usually touches one file, while a range scan must merge every level — a Bloom filter does not help there.
- An LSM delete is a tombstone: the row leaves the disk only after compaction, and until then scans read and discard those markers.
- The compaction style picks the balance: leveled (RocksDB's default) saves space at the cost of writes, tiered / universal saves writes at the cost of space and reads.
The practical point for a data engineer: an insert-and-update stream from CDC lands cheaply in an LSM store, while wide range scans for reports cost more there than they seem.