Compaction Strategies
You'll learn how wide-column stores merge and reorganize data on disk to maintain read and write performance.
When LSM trees write data to disk as SSTables, those files accumulate over time. Without cleanup, reads would need to check dozens or hundreds of files, and disk space would be wasted on duplicate or deleted records. Compaction is the background process that merges SSTables, removes obsolete data, and keeps the system efficient.
Size-Tiered Compaction Strategy (STCS)
Think of this like stacking boxes: when you have several small boxes of the same size, you combine them into one larger box.
STCS merges SSTables when you have multiple files of similar size. For example, when four 100MB files accumulate, they merge into one 400MB file. This repeats at each size tier.
Pros:
- Fast writes (less frequent merging)
- Simple to understand
Cons:
- Temporary disk space spikes (old + new files exist during merge)
- Reads may check multiple files until compaction runs
- More space amplification (duplicate data lingers longer)
Leveled Compaction Strategy (LCS)
Imagine organizing books on shelves where each shelf holds progressively more books, and books are always sorted.
LCS organizes SSTables into levels (Level 0, Level 1, Level 2...). Data moves from smaller to larger levels through targeted merges. Each level (except L0) maintains non-overlapping sorted ranges.
Pros:
- Predictable read performance (fewer files to check)
- Better space efficiency (less duplication)
Cons:
- More write amplification (data rewritten through multiple levels)
- Higher CPU and I/O overhead
Key Takeaway: Size-tiered compaction optimizes for write throughput with larger temporary space usage, while leveled compaction prioritizes consistent read performance and space efficiency at the cost of additional write overhead.