LOAD-BEARINGfree
FoundationsTier 3 · unit 1 of 12 · 13 min read

B-Tree vs LSM: how rows are stored

Here's a secret that demystifies the entire database landscape: underneath the hundreds of database products, there are basically two ways to physically store rows on disk. Once you know which one a database uses, most of its performance personality — what it's fast at, what it's slow at, when it gets weird — stops being mysterious and starts being predictable.

B-Tree: the filing cabinet (update in place)

Picture a well-organised filing cabinet with an index at the front. A B-Tree stores data in fixed-size pages (typically 4–16 KB) arranged in a balanced tree, sorted by key. The magic property: the tree is wide and shallow — even a table with billions of rows is usually only 3–4 levels deep. So finding any row means reading just 3–4 pages, and the top levels are so frequently used they're always sitting in memory anyway. Reads are fast and — crucially — predictably fast.

Writes work by finding the right page and modifying it in place — walk to the drawer, open the folder, change the paper. Two costs hide in that: it's a random write (the page could be anywhere on disk — remember from Tier 0 that random loses to sequential everywhere), and there's write amplification: changing a 200-byte row means rewriting the entire 8 KB page it lives in. There's also a safety net called the write-ahead log (WAL): before touching any page, the database appends "I'm about to change X" to a sequential log, so a crash mid-write can be replayed rather than corrupting data.

Who uses it: Postgres, MySQL/InnoDB, Oracle, SQL Server — essentially every classic relational database.

LSM tree: the inbox (append now, organise later)

Now a completely different philosophy. Instead of filing each paper immediately, throw everything into an inbox and file in batches later. An LSM tree (log-structured merge tree) takes writes into an in-memory table (plus a commit log for crash safety — pure sequential append, extremely fast). When the memory table fills, it's flushed to disk as an immutable sorted file called an SSTable — again, one big sequential write. Nothing is ever modified in place; new versions of a row just land in newer files.

Two consequences follow. First, writes are spectacular — everything the disk sees is sequential, which is the fastest thing disks do. Second, reads got harder: a row's latest version could be in the memory table or any of several files, so a read may check multiple places. Two mechanisms fight back: bloom filters — tiny probabilistic structures that answer "is key X in this file?" with a definitive no or a probable yes, letting reads skip most files instantly — and compaction, background jobs that merge files together and discard overwritten entries, keeping the file count sane.

Compaction is also the LSM's dark side: it consumes real disk I/O in bursts, and if writes outpace it, read latency develops spikes that confuse everyone ("the database is fast except when it randomly isn't"). Tuning compaction is a genuine operational skill on these systems.

Who uses it: Cassandra, RocksDB, LevelDB, HBase, ScyllaDB — the write-heavy crowd.

Choose by workload
Read-heavy, complex queriesB-Tree
Write-heavy ingest, time seriesLSM
Predictable p99 reads matter mostB-Tree
Massive write volume, tolerable read costLSM

The mental shorthand: B-Tree pays at write time to make reads cheap; LSM pays at read time (and in background compaction) to make writes cheap. Neither is better — they're the same bill, presented at different moments. Match the bill to your workload.

The trap

Benchmarking an LSM store right after bulk-loading data and declaring it fast. Of course it was fast — compaction hadn't caught up yet, and the read path hadn't accumulated its file-checking tax. The only benchmark that means anything is steady-state behaviour under sustained write load, after compaction reaches equilibrium. Marketing benchmarks conveniently skip this; your production traffic will not.

The nuance

“LSM for writes, B-tree for reads.”

A serviceable first cut and too crude to design on. A modern B-tree with write-ahead batching handles heavy write loads well, and an LSM with good bloom filters and caching serves point reads fine. The axis that actually decides is amplification: LSM trades read and space amplification for low write amplification, B-trees the reverse. Ask which one your workload can afford.

Going deeper

  • Designing Data-Intensive Applications, ch. 3 Martin Kleppmann, 2017
    The best single explanation of B-trees against LSM trees, with the write and read amplification tradeoffs drawn out properly.
  • Bigtable: A Distributed Storage System for Structured Data Chang et al., OSDI 2006
    Where the SSTable and memtable design most LSM engines still use was set out.