← Back to System Design, page 5

system-design-0087 · Hard senior

Explain write-ahead logs, and compare LSM trees with B-trees as storage engines: how does each handle writes, reads and crashes?

A write-ahead log records every change sequentially before it is applied, so after a crash the engine replays the log to recover. B-trees update fixed-size pages in place and are fast for reads and range scans, but random writes cost page writes. LSM trees buffer writes in memory, flush sorted files sequentially and merge them in the background, so they absorb high write rates at the cost of read work and compaction. Choose by workload: read-heavy and transactional for B-trees, write-heavy for LSM trees.

Full answer

Write-ahead log (WAL)

Changing data pages directly is risky: a crash halfway through writing a page leaves it corrupt. Instead, the engine first appends a record ("set row 42 balance to 900") to a log and forces it to disk (fsync). Only then is the change acknowledged. Pages are updated later, in memory, and written lazily. After a crash, the engine replays log records after the last checkpoint. Sequential appends are fast even on spinning disks, and group commit (one fsync for many transactions) raises throughput.

Both B-tree and LSM engines use a WAL. It also feeds replication (PostgreSQL streaming replication ships WAL records) and change data capture.

B-trees

Data lives in pages (often 8 to 16 KB) arranged as a balanced tree. A lookup reads about 3 to 4 pages for billions of keys, and the upper levels are usually cached. An update finds the leaf page and changes it in place; a full page splits.

  • Reads: predictable, one path down the tree; excellent range scans.
  • Writes: each small update may rewrite a whole page (write amplification), and random updates mean random I/O.
  • Used by PostgreSQL, MySQL InnoDB, SQL Server, SQLite.

LSM trees

  1. Writes go to the WAL and to an in-memory sorted table (memtable).
  2. When the memtable is full, it is written to disk as an immutable sorted file (SSTable), sequentially.
  3. Background compaction merges SSTables, dropping overwritten values and tombstones.
  4. Reads check the memtable, then SSTables from newest to oldest; Bloom filters skip files that cannot contain the key, and a sparse index finds the block.
  • Writes: sequential and batched, so very high throughput.
  • Reads: may touch several files; range scans merge several sorted runs.
  • Space and I/O: compaction rewrites data repeatedly; leveled compaction lowers read and space cost, tiered compaction lowers write cost.
  • Used by RocksDB, Cassandra, ScyllaDB, LevelDB, and many time-series stores.

Comparing

AspectB-treeLSM tree
Write throughputModerate, random I/OHigh, sequential I/O
Point readsFast and predictableGood with Bloom filters, more variable
Range scansExcellentGood, merges runs
SpaceSome fragmentationTemporary duplicates until compaction
Background workLittleCompaction can spike latency

The three amplifications summarise it: B-trees have lower read amplification, LSM trees lower write amplification, and compaction strategy moves LSM costs between write, read and space amplification.

By level

Fresher: Explain that the database writes changes to a log first so it can recover after a crash. A fresher should describe a B-tree lookup as a few page reads.

Mid: Describe LSM writes, flushes, compaction and reads with Bloom filters, and compare with B-tree updates. Name example systems.

Senior: Discuss read, write and space amplification, compaction strategies, group commit, latency spikes and workload-based choice. Expect careful reasoning about crash recovery.

Key points

  • WAL: append and fsync before acknowledging; replay after a crash
  • B-trees update pages in place; fast reads and range scans
  • LSM trees buffer in memory, flush sorted files, compact later
  • Bloom filters cut LSM read cost
  • Read, write and space amplification frame the trade-off

Common mistakes

  • Thinking LSM trees do not need a WAL
  • Claiming B-trees are always slower for writes regardless of workload
  • Ignoring compaction cost and latency spikes
  • Forgetting that deletes in LSM trees are tombstones

Follow-ups

  • How does leveled compaction differ from tiered compaction?
  • Why does PostgreSQL need VACUUM?
  • How does group commit improve throughput?

Tips

  • Explain the crash scenario that motivates the WAL
  • Walk through an LSM write and read
  • Use amplification terms in the comparison

Evaluation rubric

  • Correct WAL purpose and recovery
  • Accurate B-tree and LSM mechanics
  • Clear trade-off comparison
  • Knows real systems and compaction strategies