Skip to main content

Compaction

Compaction merges SSTables to reduce read amplification and reclaim tombstone space.

Level Structure

Loading diagram...

Leveled Strategy (10x size ratio)

LevelMaxTriggerAction
L04 filesOverflowAll L0 → overlapping L1
L1256 MBOverflowOne L1 file → overlapping L2
L22.56 GBOverflowOne L2 file → overlapping L3
L325.6 GBTimer (1h)Drop expired tombstones

Write Stalling

L0 CountEffect
≤ 4Normal. Compaction triggered.
5–8Throttled. Each write sleeps (count - 4) × 1ms.
9–12Stalled. RESOURCE_EXHAUSTED with retry-after.

Merge Process

Loading diagram...

SSTable Run Fragments

Compaction output is a run of smaller fragments, not a monolith:

sstables/L1/run-{id}/frag-0000.sst
sstables/L1/run-{id}/frag-0001.sst
sstables/L1/run-{id}/frag-0002.sst

Max temporary space = 2 × fragment_size instead of 2 × total_run_size.

Trivial move: If a fragment's key range doesn't overlap the next level, it's moved without re-merge. The executor physically copies the SSTable file from the source-level path to the target-level path, updates the manifest, then deletes the source. This is necessary because SSTable paths are level-encoded — a manifest-only update would leave the file at the old path, causing not-found errors on subsequent reads. For time-ordered keys, most compactions are trivial moves.

Tombstone Lifecycle

Loading diagram...

Range tombstones use a watermark protocol: only eligible for deletion when all levels below have been compacted past the tombstone's creation timestamp.