Introduction Cassandra compaction merges SSTables to remove deleted/expired data and reduce the number of files that must be read during queries. When compaction falls behind on write-heavy workloads, the number of SSTables grows, causing read latency to increase dramatically and disk space to expand.
Symptoms - `nodetool compactionstats` shows pending tasks growing continuously - Read latency increasing as more SSTables must be scanned - Disk usage growing beyond expected levels - `nodetool tablestats` shows high `SSTable count` per table - `Pending tasks: Compaction` in `nodetool status` is non-zero and growing
Common Causes - Write rate exceeding compaction throughput capacity - SizeTieredCompactionStrategy (STCS) creating too many SSTables - Insufficient I/O bandwidth for compaction I/O - `compaction_throughput_mb_per_sec` set too low - Large partition sizes causing slow compaction of individual SSTables
Step-by-Step Fix 1. **Check compaction status": ```bash nodetool compactionstats # Shows: # pending tasks, completed tasks, bytes compacted, active compactions
nodetool tablestats keyspace.table # Shows: SSTable count, Space used, Compaction pending ```
- 1.**Increase compaction throughput":
- 2.```bash
- 3.# Check current setting
- 4.nodetool getcompactionthroughput
# Increase (default is 16 MB/s, increase to 64 or higher) nodetool setcompactionthroughput 64
# To disable throttling entirely (use with caution) nodetool setcompactionthroughput 0 ```
- 1.**Switch to LeveledCompactionStrategy for read-heavy tables":
- 2.```sql
- 3.-- For tables with high read-to-write ratio
- 4.ALTER TABLE mykeyspace.mytable
- 5.WITH compaction = {
- 6.'class': 'LeveledCompactionStrategy',
- 7.'sstable_size_in_mb': 160
- 8.};
-- This will trigger a full compaction - monitor progress nodetool compactionstats ```
- 1.**Use TimeWindowCompactionStrategy for time-series data":
- 2.```sql
- 3.ALTER TABLE mykeyspace.metrics
- 4.WITH compaction = {
- 5.'class': 'TimeWindowCompactionStrategy',
- 6.'compaction_window_unit': 'HOURS',
- 7.'compaction_window_size': 1,
- 8.'tombstone_compaction_interval': 3600
- 9.};
- 10.
` - 11.**Manually trigger compaction for the most impacted table":
- 12.```bash
- 13.# Run major compaction during maintenance window
- 14.nodetool compact mykeyspace mytable
# Or run upgradesstable if coming from an older version nodetool upgradesstable mykeyspace mytable ```