Search DistillSys

Find a concept

Type at least two characters to search lessons, designs, papers, and interview prep.

Storageintermediate5 min read

Compaction

Rewrite immutable storage files to reclaim space and control read amplification.

2–5 minute refresher
Mental model

Merge sorted runs → discard obsolete versions → write fewer cleaner runs

30second
refresher
Compaction merges immutable sorted files, removes overwritten values when safe, and reclaims tombstoned data. It controls read and space amplification by spending background I/O and write amplification.

What problem does it solve?

An append-friendly store accumulates overlapping files and obsolete versions. Without rewriting, reads touch too many files and disk space grows indefinitely.

How it works

  1. Select files with overlap or size pressure.
  2. Merge entries in key order.
  3. Keep the newest version required by snapshots and replica repair.
  4. Apply tombstone safety rules before dropping deletes.
  5. Publish the new files atomically, then remove replaced files.
Decision guide

Key trade-offs

ChoiceWhat you gainWhat it costs
LeveledPredictable reads and spaceHigher write amplification
Size-tieredEfficient sequential writesMore read and space amplification
Aggressive cleanupReclaim disk quicklyForeground I/O interference
What happens if?

Tombstones are removed before a replica repairs

An older value can reappear when the stale replica rejoins. Keep deletions for a repair-safe interval, track replica progress, or use stronger version and anti-entropy rules.

Where it appears

  • RocksDB
  • Cassandra
  • LevelDB
  • Bigtable
Senior interview modeWhat is write amplification, and why does compaction cause it?Show answer
It is the ratio of physical bytes written to logical bytes updated. Compaction rewrites still-live data repeatedly as files merge across levels, increasing that ratio.
#compaction#sstables#storage#amplification