Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
B-trees vs LSM trees compared: in-place updates versus append-only writes, memtables, SSTables, compaction, and read, write and space trade-offs.
Quick answer: Databases use two main families of storage engine. A B-tree keeps data sorted in fixed-size pages and updates those pages in place. It gives fast, predictable reads and is the default in PostgreSQL, MySQL, SQLite and most relational databases. An LSM tree (log-structured merge-tree) never updates in place. It collects writes in memory, flushes them to disk as immutable sorted files, and merges those files in the background. It gives very fast writes and good compression and is used by RocksDB, Cassandra, ScyllaDB and LevelDB. B-trees favour reads; LSM trees favour writes.
| B-tree | LSM tree | |
|---|---|---|
| Write pattern | Random, in-place page updates | Sequential appends |
| Write speed | Good | Excellent |
| Point reads | Excellent: one path down the tree | Good: may check several files |
| Range scans | Excellent | Good, with merging across files |
| Space use | Pages are partly empty | Compact, compresses well |
| Background work | Little | Continuous compaction |
| Latency predictability | High | Can spike during compaction |
| Used by | PostgreSQL, MySQL InnoDB, SQLite, SQL Server | RocksDB, Cassandra, LevelDB, ScyllaDB, CockroachDB, TiKV |
A B-tree stores sorted keys in pages arranged as a shallow tree, as described in how database indexes work. To write:
Each write changes a page that already exists somewhere on disk, so writes land at effectively random locations. To stay safe across crashes, the change is first recorded in a write-ahead log, then applied to the page. So the data is written at least twice.
Strengths: each key lives in exactly one place. A read follows one path from root to leaf, typically three or four pages, most of them cached. Performance is steady and well understood after decades of use.
Weaknesses: random page writes were painful on spinning disks, and pages are usually only partly full, leaving wasted space.
An LSM tree turns random writes into sequential ones. The RocksDB overview describes a typical design.
Updates and deletes are just more writes. An update adds a newer version of the key. A delete adds a marker called a tombstone. Old versions are cleaned up later.
A key could be in the memtable or in any of several SSTables, so a read checks the newest data first and works backwards until it finds the key. To avoid opening every file, each SSTable has:
Left alone, SSTables would pile up, reads would slow down, and deleted data would never be freed. Compaction merges SSTables in the background: it combines sorted files, keeps only the newest version of each key, drops tombstoned data, and writes out new, larger files.
There are two common strategies:
| Strategy | How it works | Favours |
|---|---|---|
| Levelled | Files are organised into levels, each about ten times larger, with non-overlapping key ranges within a level | Reads and space efficiency |
| Size-tiered | Files of similar size are merged together | Write throughput |
Compaction is the price of the LSM design. It consumes CPU and disk, and if it falls behind, writes may be throttled or stall.
Storage engines are often compared on three ratios:
No design minimises all three at once. Tuning an engine means choosing which to sacrifice.
LSM trees were designed when random writes on spinning disks were extremely slow. SSDs made random I/O cheap, as covered in why SSDs are faster than HDDs, which narrowed the gap.
But LSM trees remain attractive on flash: their files compress well, saving expensive SSD capacity, and their sequential, lower-volume writes can reduce wear. Meta built MyRocks, a MySQL engine on RocksDB, largely for space savings.
Most of the time you do not choose directly; you choose a database, and the engine comes with it.
| Workload | Better fit |
|---|---|
| Read-heavy with many point lookups and range scans | B-tree |
| Transactional systems with mixed reads and writes | B-tree |
| Very high write volume: logs, metrics, events, time series | LSM tree |
| Storage cost matters and data compresses well | LSM tree |
| Strict, predictable latency | B-tree |
Many distributed and NoSQL databases use LSM trees because their workloads are write-heavy. Several distributed SQL databases build their SQL layer on top of an LSM key-value store.
The lines also blur. PostgreSQL is not a pure in-place engine: an update writes a new row version and leaves the old one for a background process (vacuum) to remove, a little like compaction.
A write only appends to a log and inserts into memory. Disk writes happen later, in large sequential batches, with no need to find and modify an existing page first.
A sorted string table: an immutable file of key-value pairs sorted by key, with an index and usually a Bloom filter.
The background process that merges SSTables, discarding overwritten and deleted entries so reads stay fast and space is reclaimed.
Yes. MyRocks for MySQL and several distributed SQL systems store their tables in LSM-based key-value engines.
B-trees and LSM trees answer the same question, how to keep sorted data on disk, with opposite strategies. B-trees do the work at write time to keep each key in one tidy place. LSM trees make writes cheap and defer the tidying to background compaction. Knowing which your database uses explains its performance profile, its tuning knobs and its bad days.