Most storage engines are built on one of two structures. B-trees update sorted pages in place. LSM-trees (log-structured merge trees) buffer writes in memory, flush them as immutable sorted files and merge those files in the background. This cheat sheet compares them on the points that matter when you choose a database or tune one.
B-tree in short
Keys are kept sorted in fixed-size pages, organised as a shallow, balanced tree. A lookup walks from the root to a leaf, usually three or four pages. A write finds the right leaf, changes it in memory, logs the change in the write-ahead log, and the page is written back to its place later. Full pages split in two.
Used by: PostgreSQL, MySQL InnoDB, SQLite, SQL Server, Oracle, and most relational databases.
Strengths: one place per key, predictable read latency, excellent range scans over sorted, linked leaves, mature tooling.
Weak points: random writes across the disk, a whole page rewritten for a small change, free space left in pages after splits, and maintenance such as vacuum or index rebuilds.
LSM-tree in short
A write goes to a write-ahead log and an in-memory sorted table (the memtable). When the memtable is full, it is flushed to disk as an SSTable, a sorted file that is never modified. Background compaction merges SSTables, keeps the newest version of each key and drops deleted ones. Deletes are written as tombstones.
A read checks the memtable, then SSTables from newest to oldest. Bloom filters let it skip files that cannot contain the key.
Used by: RocksDB, LevelDB, Cassandra, ScyllaDB, HBase, and the storage layer of several distributed SQL databases. MySQL can use it through MyRocks.
Strengths: sequential writes, high write throughput, dense files that compress well.
Weak points: reads may check several places, range scans merge several files, compaction competes with live traffic for disk and CPU, and deleted data lingers as tombstones until compaction.
Side by side
| B-tree | LSM-tree | |
|---|---|---|
| Write path | update page in place | memtable, then flush sorted file |
| Disk write pattern | random | sequential |
| Write throughput | good | usually higher |
| Point read | a few pages, predictable | memtable + files, Bloom filters help |
| Range scan | very good | good, merges files |
| Update / delete | change the entry | write new version / tombstone |
| Space overhead | free space in pages | old versions until compaction |
| Background work | vacuum, page cleanup | compaction |
| Latency spikes from | checkpoints, page splits | compaction, write stalls |
The amplification view
Engineers describe the trade-off with three ratios:
- Write amplification (disk bytes written per byte you write). B-trees pay with full-page writes; LSM-trees pay by rewriting data during compaction.
- Read amplification (work per read). Low and steady in B-trees; higher in LSM-trees, reduced by Bloom filters and caching.
- Space amplification (disk used versus live data). B-trees waste partly empty pages; LSM-trees keep obsolete versions until compaction, but compress well.
No design wins all three. LSM compaction strategies (leveled, size-tiered and others) shift the balance between them.
Common mistakes
- "LSM is faster." It is usually faster for writes. For read-heavy work with many range scans, a B-tree is often the better fit.
- Ignoring tombstones. In an LSM engine, deleting millions of keys does not free space right away, and scans slow down while they skip the markers.
- Judging by a short benchmark. Compaction debt and B-tree bloat build up over hours or days. Test with realistic data volumes and durations.
When to pick which
-
Pick a B-tree engine for general transactional workloads, read-heavy applications, frequent range scans and
ORDER BYon indexed columns, and when predictable latency matters more than peak write throughput. That covers most business applications. - Pick an LSM engine for write-heavy ingestion: logs, events, metrics, time series, message storage, and large key-value datasets where recent data is read most. Also when you want good compression.
- Watch compaction if you run LSM: give it disk bandwidth, monitor pending compaction, and be careful with mass deletes.
- Watch bloat if you run a B-tree engine: keep vacuum healthy and check index size on hot tables.
- Remember the layer above matters too. Many databases put a full SQL layer on top of an LSM engine, so the choice of storage engine does not decide the query language.
Originally published at abdeldjalilchougui.biz.
Top comments (0)