LSM TREE
Turn random writes into sequential ones, and keep your deletes deleted
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.
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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.