What is compaction in Cassandra and why is it important?
Learn what compaction in Cassandra is, how it merges SSTables, removes tombstones, reclaims disk space, and keeps reads fast, plus key strategies.
Expected Interview Answer
Compaction is the background process that merges multiple immutable SSTables into fewer, consolidated SSTables — combining row fragments, keeping the latest version of each column, and discarding tombstoned (deleted) and expired data.
Because Cassandra never updates data in place, every insert, update and delete accumulates across many SSTables, so a single row can be scattered across several files. Compaction reads overlapping SSTables, resolves them into one merged, sorted output, and removes obsolete versions and tombstones past their grace period. This reduces the number of SSTables a read must consult, reclaims disk space, and keeps read latency low. Strategies like SizeTieredCompactionStrategy, LeveledCompactionStrategy and TimeWindowCompactionStrategy tune this behaviour for different workloads.
- Fewer SSTables to merge per read, lowering read latency
- Reclaims disk space from overwritten and deleted data
- Physically removes tombstones after gc_grace_seconds
- Consolidates fragmented rows into contiguous storage
- Keeps Bloom filters and indexes efficient
AI Mentor Explanation
Compaction is like a statistician who, at season's end, takes a shoebox of match-by-match score slips for one player and merges them into a single clean career record, throwing away the crossed-out and duplicate slips. Without it you'd sift every slip to answer one query. Cassandra does the same: it merges scattered SSTable fragments of a row into one sorted record and discards deleted and superseded data.
Step-by-Step Explanation
Step 1
SSTables accumulate
Ongoing writes, updates and deletes create many immutable SSTables, fragmenting rows across files.
Step 2
Trigger compaction
A strategy (size-tiered, leveled, time-window) decides which SSTables to compact and when.
Step 3
Merge and resolve
Overlapping SSTables are read together; the latest value per column wins and fragments are combined.
Step 4
Purge obsolete data
Overwritten cells, and tombstones past gc_grace_seconds, are physically removed.
Step 5
Write consolidated output
A new, smaller set of SSTables is written and the old ones are deleted, reclaiming space.
What Interviewer Expects
- Definition of compaction and why it's needed
- Link to SSTable immutability and fragmentation
- That it removes tombstones after gc_grace_seconds
- Awareness of compaction strategies (STCS, LCS, TWCS)
- Its effect on read latency and disk usage
Common Mistakes
- Confusing compaction with repair or JVM garbage collection
- Thinking compaction happens synchronously on every write
- Believing tombstones are removed immediately
- Not knowing multiple compaction strategies exist
- Ignoring the temporary extra disk space compaction needs
Best Answer (HR Friendly)
“Compaction is Cassandra's background housekeeping: because it never edits files, it keeps making new ones, so scattered pieces of the same data pile up. Compaction merges those pieces into fewer, clean files, deletes what's been removed, and frees disk space, which keeps reads fast.”
Code Example
-- Time-series data suits TimeWindowCompactionStrategy
CREATE TABLE sensor_readings (
sensor_id text,
ts timestamp,
value double,
PRIMARY KEY (sensor_id, ts)
) WITH compaction = {
'class': 'TimeWindowCompactionStrategy',
'compaction_window_unit': 'DAYS',
'compaction_window_size': 1
}
AND gc_grace_seconds = 864000; -- 10 days before tombstones are purged
-- Manually trigger from the CLI:
-- nodetool compact my_keyspace sensor_readingsFollow-up Questions
- Compare SizeTiered, Leveled and TimeWindow compaction strategies.
- What is gc_grace_seconds and how does it relate to tombstones?
- Why can compaction temporarily double disk usage?
- How does compaction affect read and write amplification?
- What problems do accumulated tombstones cause for reads?
MCQ Practice
1. What is the primary purpose of compaction in Cassandra?
Compaction merges multiple SSTables into fewer files, keeping the latest data and discarding tombstones and overwritten values.
2. When are tombstones physically removed during compaction?
Tombstones are retained until gc_grace_seconds passes so deletes can propagate to all replicas; only then does compaction purge them.
3. Which compaction strategy is best suited for time-series workloads?
TimeWindowCompactionStrategy groups data into time windows, which fits append-heavy time-series data with TTLs.
Flash Cards
What is compaction? — A background process that merges multiple SSTables into fewer, keeping the latest data and discarding tombstones and overwritten cells.
Why is compaction needed? — SSTables are immutable, so rows fragment across files; compaction consolidates them, reclaims space and lowers read latency.
When are tombstones purged? — During compaction, only after gc_grace_seconds so the delete has time to reach all replicas.
Name three compaction strategies. — SizeTieredCompactionStrategy (STCS), LeveledCompactionStrategy (LCS), and TimeWindowCompactionStrategy (TWCS).