Database / Apache Cassandra Intermediate and Advanced interview questions
What is Time Window Compaction Strategy used for?
Time Window Compaction Strategy (TWCS) is purpose-built for time-series or append-mostly data where rows naturally expire together, such as metrics, logs, or IoT sensor readings written with a TTL.
- SSTables are grouped into fixed-size time windows (e.g. one window per day), and compaction only merges SSTables within the same window rather than across the whole table's history.
- Once every row in a window's SSTables has expired (past its TTL), the entire SSTable can often be dropped outright, without needing to scan or rewrite individual tombstones row by row.
- This avoids the classic time-series problem under STCS/LCS where old and new data get compacted together repeatedly, wasting I/O on data that's about to expire anyway.
CREATE TABLE iot_readings ( device_id text, reading_time timestamp, value double, PRIMARY KEY (device_id, reading_time) ) WITH compaction = {'class': 'TimeWindowCompactionStrategy', 'compaction_window_unit': 'HOURS', 'compaction_window_size': 6} AND default_time_to_live = 604800; -- 7 days
The compaction window size should roughly match your query and TTL patterns — too small creates excessive small SSTables, too large delays reclaiming disk space from expired data.
More Related questions...