Cassandra Compaction Strategies
How each compaction strategy trades read, write and space amplification, and which workload each one suits.
SSTables are immutable, so every update writes a new version and reads must merge across SSTables. Compaction merges them back together. The strategy decides how, and it is a per-table setting.
The strategies
Size-tiered (default)
Merges SSTables of similar size into larger ones. Cheap in write terms, but a partition's data can be spread across many SSTables, so reads may touch several.
Leveled
Organises SSTables into levels where each level is ten times the previous, and guarantees a partition appears in at most one SSTable per level. Reads typically touch far fewer SSTables.
The cost is write amplification: a row may be rewritten several times as it moves through the levels. Use it for tables that are read often and updated in place, and avoid it for pure write-heavy ingest.
ALTER TABLE shop.user_profiles
WITH compaction = {'class': 'LeveledCompactionStrategy', 'sstable_size_in_mb': 160};Time-window
Groups SSTables by time window and compacts only within a window. Once a window is closed, its SSTables are never rewritten.
This is the right strategy for time-series data with TTL: an entire window expires and its SSTables are dropped whole, without the rewriting that other strategies do.
CREATE TABLE metrics (
device_id uuid, bucket date, ts timestamp, value double,
PRIMARY KEY ((device_id, bucket), ts)
) WITH compaction = {
'class': 'TimeWindowCompactionStrategy',
'compaction_window_unit': 'DAYS',
'compaction_window_size': 1
}
AND default_time_to_live = 7776000
AND gc_grace_seconds = 3600;Monitoring compaction
nodetool compactionstats # what is running and how much remains
nodetool tablestats shop.events # SSTable count per table
nodetool tablehistograms shop events # SSTables touched per readA rising SSTable count and a growing pending-compaction backlog mean compaction cannot keep up with
the write rate. Options: raise compaction_throughput_mb_per_sec, add
concurrent_compactors, use faster storage, or reduce the write rate.
nodetool setcompactionthroughput 128 # MB/s, takes effect immediately