When choosing an LSM compaction strategy, match LCS to read-heavy/disk-tight KV stores, STCS to write-heavy workloads, and FIFO to TTL logs.
TL;DR30-second simplified summary▾
STCS minimizes write amplification at ~10x but needs 50% disk headroom; LCS bounds space at 1.11x with non-overlapping key ranges; write-stalls cap L0 overlap.
P · Problem & The Uncompacted SSTable Pileup.
Every MemTable flush creates a brand-new immutable SSTable on disk. Over a day of writes, an engine can flush hundreds of overlapping SSTables.
Without compaction, a GET(key) or range scan must merge-iterate across all 500 SSTables, and overwrites or tombstones (DELETE) never reclaim disk space. Yet running compaction greedily on every flush rewrites gigabytes of cold data repeatedly—burning out NVMe SSD endurance.
Size-Tiered (Low Write Amp) vs. Leveled (Tight Space & Fast Reads)
- ~10x write amplification
- Needs ~50% free SSD headroom
- Reads check overlapping tiers
- ~30x write amplification
- Only ~10% space overhead (1.1x)
- 1 file per level (non-overlapping)
R · Representation: Size-Tiered vs. Leveled Geometry.
In STCS, 4 small 64 MB SSTables merge into one 256 MB SSTable; 4 x 256 MB merge into 1 GB, and so on. Any key can appear in multiple tiers simultaneously.
In LCS, L0 contains freshly flushed overlapping SSTables. Once L0 reaches a threshold (e.g., 4–8 files), it merges into L1 (e.g., 10 MB total), which partitions keys into sorted, non-overlapping SSTables ([A..D], [E..H], ...). Each subsequent level L_i is 10x larger than L_{i-1} (L2 = 100 MB, L3 = 1 GB, L4 = 10 GB).
Overlapping L0 -> Non-Overlapping L1 (10MB) -> L2 (100MB) -> L3 (1GB)
I · Invariant: Write vs. Space Amplification Math.
When a 100 MB SSTable from L2 merges down into L3 (1,000 MB), it overlaps with roughly T = 10 SSTables in L3.
That means moving 100 MB down one level reads and rewrites ~1,000 MB (T = 10x write amplification per level). Across 3–4 level transitions, LCS write amplification totals ~30x–40x, whereas STCS only rewrites each byte once per tier (~10x).
Pick L2 SSTable -> Find Overlapping L3 Files -> Merge-Sort -> Atomic Manifest Swap
M · Mechanics at Scale: L0 Pileups, Write Stalls & Hybrid Tiered-Leveled.
If client writes arrive faster than background compaction can drain L0 into L1, L0 accumulates dozens of overlapping files.
Once L0 hits a soft limit (e.g., 20 files), RocksDB applies smooth write-stalls (rate-limiting incoming writes) and hard-stops writes at 36 files so read latency doesn't degrade 10x.
Normal (<12 L0) -> Soft Delay (20 L0) -> Hard Stall (36 L0) -> Recovered
- Problem
- How to pick an LSM compaction strategy that balances SSD endurance, disk headroom, and p99 read latency.
- Naive Solution
- Leave STCS enabled on a read-heavy or 85%-full SSD cluster.
- Why It Fails
- STCS runs out of disk space during major compactions (needs 50% free headroom) and suffers slow point/range reads across overlapping tiers.
- Alternatives
- Size-Tiered (STCS) vs. Leveled (LCS) vs. FIFO.
- Engineering Decision
- Default to Leveled Compaction (LCS) for balanced/read-heavy KV workloads, STCS only for write-dominated append workloads with >=50% free disk, and FIFO for pure TTL logs.
- Trade-Offs
- LCS trades ~3x higher SSD write amplification for 1.1x space efficiency and predictable O(L) point reads.
E · Edge Cases, Assumptions & When NOT to Use LCS.
Deletes in an LSM-Tree write a tombstone marker—space is NOT freed until compaction reaches the bottom-most level (L_max) where the original key lives.
When NOT to use Leveled Compaction: if your workload is 95%+ blind appends/sensor logs with fixed 7-day TTL and zero updates, LCS burns 30x SSD endurance rewriting logs down 4 levels for nothing—use FIFO compaction (1.0x write amp) or time-windowed compaction (TWCS) instead!
LCS (General KV) vs. STCS (Write-Heavy) vs. FIFO (TTL Logs)
A cluster running Size-Tiered Compaction (STCS) is at 72% SSD utilization with a single 800 GB Tier-3 SSTable and 3 more 800 GB SSTables about to compact. What happens when compaction triggers?
Practice: Make the trade-off yourself.
Why does Leveled Compaction (LCS) with a level size ratio of `T = 10` keep space amplification near `1.11x`?