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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
