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. 1.**Increase compaction throughput":
  2. 2.```bash
  3. 3.# Check current setting
  4. 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. 1.**Switch to LeveledCompactionStrategy for read-heavy tables":
  2. 2.```sql
  3. 3.-- For tables with high read-to-write ratio
  4. 4.ALTER TABLE mykeyspace.mytable
  5. 5.WITH compaction = {
  6. 6.'class': 'LeveledCompactionStrategy',
  7. 7.'sstable_size_in_mb': 160
  8. 8.};

-- This will trigger a full compaction - monitor progress nodetool compactionstats ```

  1. 1.**Use TimeWindowCompactionStrategy for time-series data":
  2. 2.```sql
  3. 3.ALTER TABLE mykeyspace.metrics
  4. 4.WITH compaction = {
  5. 5.'class': 'TimeWindowCompactionStrategy',
  6. 6.'compaction_window_unit': 'HOURS',
  7. 7.'compaction_window_size': 1,
  8. 8.'tombstone_compaction_interval': 3600
  9. 9.};
  10. 10.`
  11. 11.**Manually trigger compaction for the most impacted table":
  12. 12.```bash
  13. 13.# Run major compaction during maintenance window
  14. 14.nodetool compact mykeyspace mytable

# Or run upgradesstable if coming from an older version nodetool upgradesstable mykeyspace mytable ```

Prevention - Monitor compaction pending tasks with alerting - Choose the right compaction strategy: STCS for write-heavy, LCS for read-heavy, TWCS for time-series - Size `compaction_throughput_mb_per_sec` based on disk I/O capacity - Keep partition sizes under 100MB to avoid slow compaction - Monitor disk I/O utilization during compaction (should be under 80%) - Use `nodetool compactionhistory` to track compaction trends - Plan capacity for 2-3x the raw data size to accommodate compaction overhead