LSM TREE

Turn random writes into sequential ones, and keep your deletes deleted

L216 MINDATABASES
PREDICT

An LSM tree stores data in immutable sorted files (SSTables); a read checks them newest to oldest, and a delete is written as a tombstone marker. Compaction merges SSTables in the background. x=1 sits in the oldest SSTable. You DELETE x, which lands as a tombstone in the newest SSTable. Compaction now merges only the two newest SSTables. May it drop the tombstone?

An LSM tree never updates data in place. A write goes into a sorted in-memory memtable. When that fills, it is flushed to disk as an immutable sorted file, an SSTable. A read checks the memtable, then SSTables from newest to oldest, and the first table that mentions the key wins.

A delete is a write too: a tombstone that shadows older values. Compaction merges SSTables in the background, keeping only the newest entry per key. Two rules keep it correct. Log each write to a WAL before you acknowledge it. Keep a tombstone until no older table can still hold the key.

▶ SIMULATION
CLIENT
acknowledged: (empty)
last read: —
—
MEMORY + LOG
memtable: (empty)
WAL: off
crashes 0 · replayed 0
SSTABLES ON DISK
(none)
acked but unreadablenone
deleted but readablenone
✓ acknowledged writes are readable, crash or not✓ deleted keys never come back
SCHEDULE
01[CLIENT]PUT a=1
02[CLIENT]PUT b=2
03[CLIENT]PUT c=3
04[ENGINE]flush memtable → new SSTable
05[CLIENT]PUT a=9
06[CLIENT]DELETE b (writes a tombstone)
07[CLIENT]GET a
08[CLIENT]GET b
09[CLIENT]GET c
10[ENGINE]flush memtable → new SSTable
11[CLIENT]GET a
12[CLIENT]report
Start of write, flush, read scenario. 12 steps.
Log-structured merge tree
===================================================

WRITE(k, v)
  1. append (k, v) to the WAL and fsync      <- durability
  2. insert into the memtable (sorted, in memory)
  3. acknowledge
  DELETE(k) is a write of a tombstone (k, †)

FLUSH (memtable full)
  write it out as an immutable sorted SSTable, then truncate the WAL

READ(k)
  memtable, then SSTables newest -> oldest
  the FIRST table that mentions k decides:
    a value    -> return it
    a tombstone -> "not found", stop (do not look further)
  (Bloom filters let a read skip tables that cannot hold k)

COMPACT(newer, older)
  merge-sort; for each key the newer entry wins
  a tombstone may be dropped ONLY if no older SSTable below
  the merge can still hold the key (e.g. the merge reaches the
  bottom level); otherwise keep it, or the old value resurrects

Trade-off: sequential writes and cheap ingest, paid for with
read amplification (many tables to probe) and write amplification
(data rewritten by compaction).

Used by: RocksDB, LevelDB, Cassandra, ScyllaDB, HBase

Watch the two invariants. CRASH, NO WAL loses three acknowledged writes. FIX: WAL REPLAY loses nothing under the same crash. COMPACTION DROPS TOMBSTONE brings x back from the oldest SSTable. FIX: KEEP TOMBSTONE runs the same compaction and keeps x deleted.

BREAK IT NOT YET

This engine runs without a write-ahead log. Every PUT and DELETE is acknowledged as soon as it reaches the memtable. Insert one crash so that something the client was told succeeded is gone after restart.

GOAL: an acknowledged write or delete is lost after the restart

FIX IT NOT YET

Implement the read path and compaction. Tables are plain objects; a value of null is a tombstone. get(key, memtable, sstables) checks the memtable, then sstables newest-first, and returns the value or null; a tombstone ends the search. compact(newer, older, olderBelow) merges two tables (newer wins). When olderBelow is false it must drop tombstones, since nothing older is left for them to hide; when it is true it must keep them.

Mental model:

an LSM tree is a stack of immutable sorted files, read newest first. Nothing is ever overwritten. Newer entries shadow older ones, so a delete is a tombstone that must outlive every older copy of the key. The memtable is memory, so a write is safe only once the WAL has it.

IN PRODUCTION

RocksDB and LevelDB (and everything built on them: MyRocks, TiKV, Kafka Streams state stores), Pebble (CockroachDB's own Go engine, modelled on RocksDB), Cassandra, ScyllaDB and HBase. Symptoms: read latency that grows with the number of SSTables, compaction stealing disk bandwidth, tombstone-heavy reads timing out in Cassandra, and deleted data reappearing when tombstones are purged before every replica has seen them.

IN INTERVIEWS

“Design a write-heavy key-value store” or “LSM tree vs B+ tree?” Walk the write path (WAL, memtable, flush), the read path (newest first, Bloom filters), and compaction. Name the trade: cheap sequential writes for read and write amplification. Bonus points for explaining tombstones and when they can be dropped.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: Query Planning →