Database / Apache Cassandra Intermediate and Advanced interview questions
What are the different compaction strategies available in Cassandra?
Compaction merges multiple SSTables into fewer, larger ones, discarding overwritten data and expired tombstones along the way. Cassandra offers a few strategies, each tuned for a different workload shape.
| STCS | LCS | TWCS |
| Size-Tiered: merges similarly-sized SSTables together. | Leveled: organizes SSTables into non-overlapping levels. | Time Window: groups SSTables by time bucket. |
| Good for write-heavy workloads. | Good for read-heavy workloads needing predictable latency. | Good for time-series data with TTL expiration. |
| Can cause temporary space amplification (needs free disk equal to data size). | More consistent read performance, but higher compaction I/O. | Whole SSTables expire together, minimizing tombstone scanning. |
ALTER TABLE metrics WITH compaction = {'class': 'TimeWindowCompactionStrategy', 'compaction_window_unit': 'DAYS', 'compaction_window_size': 1};
Choosing the right strategy is a workload decision, not a one-size-fits-all default: STCS suits general write-heavy tables, LCS suits tables with heavy read amplification concerns, and TWCS is purpose-built for append-only, time-ordered data with TTLs.
More Related questions...