Compaction
Rewrite immutable storage files to reclaim space and control read amplification.
2–5 minute refresher
Write→WAL→Memtable→ flush →SSTablesbackground compaction ↓Sorted levels
Merge sorted runs → discard obsolete versions → write fewer cleaner runs
30second
refresher
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
- Select files with overlap or size pressure.
- Merge entries in key order.
- Keep the newest version required by snapshots and replica repair.
- Apply tombstone safety rules before dropping deletes.
- Publish the new files atomically, then remove replaced files.
Decision guide
Key trade-offs
| Choice | What you gain | What it costs |
|---|---|---|
| Leveled | Predictable reads and space | Higher write amplification |
| Size-tiered | Efficient sequential writes | More read and space amplification |
| Aggressive cleanup | Reclaim disk quickly | Foreground 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.