Skip to content

Lesson 3 of 9 · 24m

LSM Trees and Compaction

Log-structured storage engines optimize for writes by turning random mutations into sequential appends, then paying the cost later during compaction.

B-trees update pages in place. LSM trees take the opposite bet: make writes cheap by appending them, organize them into immutable sorted files, and clean up the mess later in compaction. Cassandra, RocksDB, LevelDB, ScyllaDB, and many time-series systems are built on this idea.

The write path

write request
  -> commit log / WAL (durability)
  -> memtable (sorted in memory)
  -> flush immutable memtable to SSTable on disk
  -> compact SSTables in the background

The memtable is an in-memory sorted structure. When it fills, it is flushed as an immutable sorted run. Because each run is sorted, reads can binary search within it; because runs are immutable, the engine can cache and compact them safely.

Reads fan out until compaction cleans up

A key may exist in the memtable, a newer SSTable, an older SSTable, or nowhere at all. Reads therefore check multiple levels unless metadata can rule files out. Bloom filters answer "definitely not here" quickly so most files are skipped.

L0:  sst_104  sst_103  sst_102   newest, overlapping key ranges
L1:  [a-f] [g-m] [n-s] [t-z]    non-overlapping sorted runs
L2:  larger, colder runs

This is the central LSM trade: writes are easy, but reads may do more work unless compaction keeps the structure tidy. Size-tiered compaction favors write throughput; leveled compaction favors read predictability and lower space amplification.

Deletes are tombstones

An LSM delete usually writes a marker instead of erasing bytes immediately:

put(user:42, {...})     in newer sstable
delete(user:42)         tombstone in even newer sstable

The tombstone must survive until compaction knows no older copy remains. If tombstones accumulate — say a queue table constantly inserts and deletes — reads slow down and disk usage climbs even though the logical table is empty. This is not a bug in the database; it is the cost model working exactly as designed.

Compaction is a capacity-planning problem

Compaction rewrites files, so it needs spare disk bandwidth and free space. If ingest outruns compaction, the number of small files explodes and read latency falls off a cliff. Operators watch write amplification, compaction backlog, and read latency together.

# RocksDB-style tuning knobs you will see in real systems
max_background_jobs: 8
write_buffer_size: 67108864
max_write_buffer_number: 4
level0_file_num_compaction_trigger: 4

Field notes

  • Never tune compaction in isolation; ingest rate, read SLOs, and disk headroom are one system.
  • Hot keys can be painful in any engine, but LSMs add compaction pressure if the same keys are rewritten constantly.
  • Large values are often better stored outside the LSM and referenced by key; compaction otherwise copies megabytes repeatedly.
  • Tombstone-heavy workloads need explicit deletion design: TTLs, partitioning by time, or dropping whole partitions instead of row-by-row deletes.

Was this lesson helpful?

Sign in to rate