Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Compare size-tiered and leveled compaction in an LSM-based store such as Cassandra or RocksDB in terms of read, write and space amplification. A Cassandra table that stores a work queue with many deletes has rising read latency and warnings about scanning many tombstones. What is happening and what do you change?

Size-tiered compaction merges SSTables of similar size, so it rewrites data rarely (low write amplification) but leaves a key spread over many files (high read amplification) and needs a lot of free space for big merges. Leveled compaction keeps non-overlapping files per level, so a read touches about one file per level and space stays tight, at the cost of rewriting data many more times. The queue table is slow because consumers delete from the head of a partition and each read must scan thousands of tombstones that Cassandra keeps for gc_grace_seconds, 10 days by default; the fix is to stop using Cassandra as a queue, or model the data in time buckets with TTLs and TimeWindowCompactionStrategy.

Why this question is asked

Compaction strategy is the main tuning choice in an LSM database, and getting it wrong shows up months later as slow reads, full disks or melting I/O. The work-queue pattern is the most famous way to hurt Cassandra, and many teams have learned it the hard way. Interviewers combine the two because both require understanding what compaction does with versions and tombstones. In this answer, we will recap the three kinds of amplification, compare size-tiered and leveled compaction, explain why the queue table collects tombstones, and fix it, with the metrics that would have warned us earlier.

1A Quick Recap of the Three Amplifications

An LSM store, as we know, writes immutable sorted files called SSTables and merges them in the background. Three ratios describe the cost of a design. Write amplification is how many bytes are written to disk for each byte the application writes, because compaction rewrites data again and again. Read amplification is how many files or blocks a read must check to find a key. Space amplification is how much disk is used compared with the size of the live data, because old versions and tombstones stay until compaction removes them. No strategy minimises all three; each picks a balance.

2Size-Tiered Compaction

Size-tiered compaction (STCS, the long-time default in Cassandra) waits until there are several SSTables of similar size, four by default, and merges them into one larger file. Small files become medium files, four medium files become a large one, and so on. Each piece of data is rewritten only once per tier, so write amplification is low, which suits write-heavy tables.

The costs are on the other two sides. A key that is updated over time can have versions in many files of different tiers, so a read may consult many SSTables. Old versions linger until their files happen to be merged with the newer ones, so space use is higher. And merging the four largest files needs room for the output while the inputs still exist, so in the worst case a table needs about as much free space as data.

3Leveled Compaction

Leveled compaction (LCS) organises SSTables into levels. Level 0 receives flushed memtables. From level 1 on, each level holds files with non-overlapping key ranges, with small files, 160 MB by default in Cassandra, and each level about ten times larger than the one before. When a level is over its size, a file is merged into the overlapping files of the next level.

Because files in a level do not overlap, a key can be in at most one file per level, so a read checks about one file per level, and most reads are served by a single SSTable. Old versions are cleaned up quickly, so space amplification is low, around 10 percent. The price is write amplification: a piece of data is rewritten each time it moves down a level, and each move can rewrite about ten times its size of overlapping data, so leveled compaction can write many times more bytes than size-tiered. It suits read-heavy tables with updates.

Size-tiered versus leveled compaction
Size-tiered (STCS)Leveled (LCS)
mergesabout 4 files of similar sizea file into overlapping files of next level
write amplificationlowhigh, about 10x per level moved
read amplificationhigh, a key can be in many fileslow, about one file per level
space amplificationhigh, needs up to 50% free disklow, about 10% overhead
good forwrite-heavy, rarely read tablesread-heavy tables with updates

4Why the Queue Table Is Full of Tombstones

A queue in Cassandra usually looks like a partition per queue, with rows clustered by enqueue time. Producers insert at the end; consumers read the oldest rows and delete them once processed. In a relational database that might be fine. In an LSM store, a delete does not remove anything: it writes a tombstone, and the tombstone must stay for gc_grace_seconds, 864,000 seconds or 10 days by default, so that replicas that missed the delete can learn about it during repair.

Now consider the consumer's query, SELECT ... WHERE queue_id = 'emails' LIMIT 100. The rows it wants are after all the processed ones, so Cassandra must read past every tombstone of the last ten days before finding live rows. With 100,000 processed messages per day that is a million tombstones per read. Each read gets slower, the logs show warnings when a read scans more than tombstone_warn_threshold tombstones (1,000 by default), and past tombstone_failure_threshold (100,000 by default) the read fails.

SQL
-- The anti-pattern
CREATE TABLE work_queue (queue_id text, enqueued timeuuid, payload text, PRIMARY KEY (queue_id, enqueued));
DELETE FROM work_queue WHERE queue_id = 'emails' AND enqueued = 5b6e...; -- writes a tombstone
SELECT * FROM work_queue WHERE queue_id = 'emails' LIMIT 100;
-- WARN Read 100 live rows and 48213 tombstone cells for query SELECT ... (see tombstone_warn_threshold)

5Fixing It

The cleanest fix is to use a real queue. Kafka, RabbitMQ or a cloud queue service are built for consuming and acknowledging messages, and Cassandra can keep the durable record of each job if we need one. If the data must stay in Cassandra, we change the model so that reads never walk over deleted data. We bucket the partition by time, for example (queue_id, hour), so each partition holds one hour of messages and consumers move from bucket to bucket; we mark rows as processed instead of deleting them, or let a TTL expire them; and we choose TimeWindowCompactionStrategy, which groups SSTables by time window, so whole expired windows can be dropped as files instead of being compacted row by row.

We should resist the tempting quick fix of lowering gc_grace_seconds to purge tombstones sooner. It is safe only if repairs complete on every replica well within the new window, because a tombstone purged before a replica has seen it lets the deleted data come back. So we first make sure repairs run reliably, and only then shorten the window if needed.

Java
-- Time-bucketed, TTL-based model
CREATE TABLE jobs_by_hour (queue_id text, hour timestamp, enqueued timeuuid, payload text,
PRIMARY KEY ((queue_id, hour), enqueued))
WITH default_time_to_live = 172800 -- rows expire after 2 days
AND compaction = {'class': 'TimeWindowCompactionStrategy',
'compaction_window_unit': 'HOURS', 'compaction_window_size': 6};
Step throughDiagnosing slow reads in a Cassandra table
  1. Read latency rising?per-table metrics
    YesCheck SSTables per read and tombstonesNoKeep watching
  2. Tombstone warnings in logs?tombstone_warn_threshold
    YesFix the data model: buckets, TTL, TWCSNoNext check
  3. Many SSTables per read?nodetool tablehistograms
    YesReview compaction strategy and backlogNoLook at other causes

3 steps in order. Play them, or step through them one at a time.

6What to Watch

The warning signs are visible long before failures. The tombstone_warn_threshold messages in the logs name the query and the table. nodetool tablestats reports per-table read latency and the average and maximum tombstones per slice, and nodetool tablehistograms shows how many SSTables each read touched. Rising SSTables per read points at compaction falling behind or a poor strategy; rising tombstones per read points at the data model. Pending compactions should also be on the dashboard, because a backlog makes both worse.

7Follow-up questions

Why is lowering gc_grace_seconds risky?

gc_grace_seconds is the time a tombstone must survive so every replica can receive it, through normal writes, hints or repair. If a replica was down and missed the delete, and the other replicas purge the tombstone before repair reaches it, that replica still holds the old value. The next repair or read repair sees live data on one replica and no tombstone anywhere, and copies the deleted data back: so-called zombie data. Lowering it is safe only when repairs reliably complete within the shorter window.

When would you choose leveled compaction anyway?

For tables that are read much more than written and whose rows are updated, such as user profiles, because reads hit about one SSTable and space stays tight. We accept the higher write amplification and check that disks have the I/O bandwidth for it.

Does TTL avoid tombstones?

Not entirely: an expired cell behaves like a tombstone until it is compacted away. But with TimeWindowCompactionStrategy, data with the same TTL written in the same window expires together, so whole SSTables can be dropped at once, which is far cheaper than compacting individual tombstones.

8Conclusion

In this answer, we saw that size-tiered compaction merges similar-sized files with low write amplification but high read and space amplification, while leveled compaction keeps non-overlapping levels for cheap reads and tight space at the cost of much more rewriting. A queue in Cassandra deletes from the head of a partition, and because tombstones live for gc_grace_seconds, every read scans thousands of them. The fixes are a real queue, or time buckets with TTLs and TimeWindowCompactionStrategy, with repairs in place before any change to gc_grace_seconds, and tombstone and SSTables-per-read metrics on dashboards.

What to say in the interviewAbout 100 seconds aloud

Open with

Size-tiered compaction merges about four SSTables of similar size into a bigger one. Data is rewritten rarely, so write amplification is low, but a key can live in many files, so reads check many SSTables, old versions linger, and a big merge can need as much free space as the data.

Then, if they want more

Leveled compaction keeps each level as non-overlapping files, each level ten times the previous. A read checks about one file per level, space overhead is around ten percent, but data is rewritten every time it moves down a level, so write amplification is much higher. So size-tiered for write-heavy tables, leveled for read-heavy tables with updates.

The queue table is the classic tombstone problem. Consumers delete processed rows at the head of the partition, but a delete in Cassandra writes a tombstone, which must stay for gc_grace_seconds, ten days by default, so replicas that missed the delete can be repaired.

Every consumer read starts at the head and scans all the tombstones before reaching live rows, which is why latency rises and the logs warn about tombstone_warn_threshold; at the failure threshold reads fail. I would move the queue to Kafka or a real queue service.

If it must stay, I would bucket partitions by time, use TTLs instead of deletes, and TimeWindowCompactionStrategy so expired windows are dropped as whole files. I would not just lower gc_grace_seconds unless repairs reliably finish inside it, otherwise deleted data can come back. And I would watch tombstones per read and SSTables per read.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare size-tiered and leveled compaction in an LSM-based store such as Cassandra or...": https://scrivio-interview-library.netlify.app/databases/storage-internals/e563f2f3d1/