Compaction Strategies
What you'll learn: How Cassandra merges SSTables efficiently through different compaction strategies: Size-Tiered, Leveled, and Time-Window.
Why Compaction Matters
Remember that Cassandra writes to immutable SSTables. Over time, you accumulate many small SSTables containing overlapping or outdated data. Compaction is the background process that merges these SSTables, removes deleted or expired data, and reduces disk usage and read amplification.
Three Core Strategies
Size-Tiered Compaction (STCS):
Think of organizing books by stacking similar-sized piles. When you have ~4 SSTables of roughly the same size, merge them into one larger SSTable. This is write-optimized—great for write-heavy workloads—but reads may scan many SSTables before compaction occurs.
Leveled Compaction (LCS):
Imagine organizing books on shelves by topic and level, with each shelf holding fixed-size volumes. SSTables are organized into levels (L0, L1, L2...), with each level holding non-overlapping data ranges. When L0 fills up, data is promoted and merged into L1, and so on. This keeps read performance predictable but requires more frequent compaction (more write amplification).
Time-Window Compaction (TWCS):
Perfect for time-series data. SSTables are grouped by time windows (e.g., hourly or daily). Old windows are compacted once and never touched again. When data expires via TTL, entire SSTables can be dropped instantly—no need to rewrite data.
Choosing Your Strategy
- STCS: Default, good for mixed or write-heavy workloads
- LCS: Better read performance, use when reads dominate and disk space is limited
- TWCS: Ideal for time-series data with TTL expiration
Key Takeaway:
Compaction strategies balance write amplification, read performance, and disk space by controlling how and when SSTables are merged—choose based on your workload pattern.