Skip to content

Lesson 3 of 9 · 26m

LSM Trees: Memtables, SSTables, Compaction, and Bloom Filters

See why log-structured merge trees ingest writes quickly, how levels and compaction trade space for read cost, and why deletes are not free.

LSM trees exist because random writes are expensive. Instead of updating a B-tree page in place, an LSM storage engine buffers writes in memory, appends them to a log for durability, and later writes sorted runs to immutable files. The design turns many random writes into sequential batch writes — then pays the bill during compaction.

The write path is deliberately indirect

RocksDB, Cassandra, ScyllaDB, and many newer engines share the same broad shape:

write -> memtable
      -> write-ahead log

memtable fills -> immutable memtable -> flush -> SSTable level 0
background compaction -> level 1 -> level 2 -> deeper levels

A memtable is usually an ordered in-memory structure such as a skiplist. Once full, it becomes immutable and is flushed as a sorted string table, or SSTable. Because files are immutable, flushing can be sequential and cache-friendly.

Reads must reconcile versions

The newest value may live in the memtable, an immutable memtable, or one of several SSTables. Reads check newer structures first and merge what they find. That means a point lookup can touch more than one file unless metadata helps narrow the search.

get('user:42')
  1. active memtable
  2. immutable memtables
  3. level 0 SSTables
  4. deeper levels, guided by key ranges and Bloom filters

A Bloom filter answers "this key is definitely absent" or "possibly present" without reading the data block. False positives cost extra I/O, but false negatives are not allowed.

Compaction is the price of admission

Multiple SSTables accumulate. Compaction merges sorted files, discards overwritten versions, resolves tombstones, and rewrites data into fewer, better-organized files. This is essential maintenance, but it consumes CPU, memory, disk bandwidth, and temporary space.

level 0: file A file B file C
             \   |   /
              compact
                 v
level 1:      larger sorted run

Leveled compaction minimizes read amplification but rewrites data more aggressively. Size-tiered compaction reduces write amplification but can keep more overlapping runs, making reads and space usage worse. There is no universal best policy; there is only the workload.

Deletes write tombstones

In an immutable-file design, deletion cannot simply erase the old bytes. The engine writes a tombstone marking the key as deleted. During compaction, the tombstone suppresses older versions and eventually disappears when no lower-level data can still contain them.

put('k', 'v1')   in SSTable 3
delete('k')      tombstone in newer SSTable 1
compact          both entries eventually collapse away

High delete churn can create large numbers of tombstones. Range scans then scan through deletion markers, and compaction falls behind. Operators often describe the symptom as "the database got slower even though traffic was flat." The real cause was accumulating deletion debt.

Choose B-tree or LSM by shape, not fashion

B-trees tend to favor read-heavy workloads, ordered scans, and update-in-place locality. LSMs tend to favor heavy write ingestion and sequential key patterns where compaction can keep up. Hybrid systems blur the line. The correct question is always: what does the engine do per write, per read, and per byte of maintenance?

Field notes

  • Compaction falling behind can raise read latency before disk space alarms fire.
  • Bloom filters reduce useless reads but do not make reads free.
  • Immutable files simplify replication and snapshots because old data does not mutate underneath readers.
  • Large values stored beside small keys can destroy compaction efficiency; some engines separate value logs for this reason.

Was this lesson helpful?

Sign in to rate