Cassandra Compaction Strategies on 5.0: STCS, LCS, TWCS, UCS

Updated

Apache Cassandra compaction strategy illustration

On Cassandra 5.0.9 with the configuration file that actually ships as cassandra.yaml, a new table is still created with SizeTieredCompactionStrategy:

$ cqlsh -e "CREATE TABLE hw_c1.default_probe (id uuid PRIMARY KEY, value text);
            DESCRIBE TABLE hw_c1.default_probe;"

    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}

UnifiedCompactionStrategy (UCS) does ship in 5.0 — CASSANDRA-18397, CEP-26 — but it is opt-in. It becomes the default only when default_compaction is set, which the stock file leaves commented out:

$ grep -A4 default_compaction /opt/homebrew/etc/cassandra/cassandra.yaml
# default_compaction:
#   class_name: SizeTieredCompactionStrategy
#   parameters:
#     min_threshold: 4
#     max_threshold: 32

How to actually get UCS as the default

Cassandra 5.0 ships two configuration files. cassandra_latest.yaml explains itself in its own header:

#     - cassandra.yaml: Contains configuration defaults for a "compatible"
#       configuration that operates using settings that are backwards-compatible
#       and interoperable with machines running older versions of Cassandra.
#     - cassandra_latest.yaml: Contains configuration defaults that enable
#       the latest features of Cassandra ...
#       To use this version, simply copy this file over cassandra.yaml, or specify
#       it using the -Dcassandra.config system property

That second file is where UCS is the default:

$ grep -A7 '^default_compaction' /opt/homebrew/etc/cassandra/cassandra_latest.yaml
default_compaction:
  class_name: UnifiedCompactionStrategy
  parameters:
    scaling_parameters: T4
    max_sstables_to_compact: 64
    target_sstable_size: 1GiB
    sstable_growth: 0.3333333333333333
    min_sstable_size: 100MiB

Adding exactly that block to the scratch node’s cassandra.yaml and restarting changes what a brand-new table gets — and leaves existing tables alone, because compaction is stored per table in the schema:

-- table created AFTER the restart
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.UnifiedCompactionStrategy', 'max_sstables_to_compact': '64', 'min_sstable_size': '100MiB', 'scaling_parameters': 'T4', 'sstable_growth': '0.3333333333333333', 'target_sstable_size': '1GiB'}

-- table created BEFORE the restart, unchanged
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}

What UCS is

UCS does not adjust itself to the data distribution or the workload. It is a single parameterised strategy with one static knob, and the server tells you its grammar when you get it wrong:

$ cqlsh -e "CREATE TABLE hw_c1.ucs_bad2 (id uuid PRIMARY KEY, v text)
            WITH compaction = {'class':'UnifiedCompactionStrategy','scaling_parameters':'Z9'};"
ConfigurationException: Scaling parameter Z9 must match N|L[0-9]+|T[0-9]+|[+-]?[0-9]+

T4 behaves like tiered compaction with a fanout of 4, L10 like leveled, N sits between them. You choose; nothing is detected for you, and there is no time-window mode. UCS also refuses LCS’s vocabulary:

ConfigurationException: Properties specified [sstable_size_in_mb] are not understood by UnifiedCompactionStrategy

STCS, LCS and TWCS, watched doing their jobs

Four batches of 1,000 rows, each followed by nodetool flush, into an STCS table with the default min_threshold: 4:

after flush 1: SSTable count: 1
after flush 2: SSTable count: 2
after flush 3: SSTable count: 3
after flush 4: SSTable count: 1

The fourth flush hits the threshold and the four files become one. The same four batches into a Leveled table produce the level layout nodetool tablestats only prints for LCS:

		Table: lcs_demo
		SSTable count: 1
		SSTables in each level: [1, 0, 0, 0, 0, 0, 0, 0, 0]
		SSTable bytes in each level: [72657, 0, 0, 0, 0, 0, 0, 0, 0]

TWCS is the one that visibly refuses to merge. Four batches written with USING TIMESTAMP one day apart, into a table with a one-day compaction window, stay as four SSTables no matter how long you wait:

SSTable count: 4
nb-1-big-Data.db: Minimum timestamp: 08/06/2026 02:06:40  Maximum timestamp: 08/06/2026 02:14:59
nb-2-big-Data.db: Minimum timestamp: 08/07/2026 02:06:40  Maximum timestamp: 08/07/2026 02:14:59
nb-3-big-Data.db: Minimum timestamp: 08/08/2026 02:06:40  Maximum timestamp: 08/08/2026 02:14:59
nb-4-big-Data.db: Minimum timestamp: 08/09/2026 02:06:40  Maximum timestamp: 08/09/2026 02:14:59

That is the entire point of TWCS: old windows are never rewritten, so a whole day’s SSTable can be dropped when its TTL expires instead of being merged forever. It is also why TWCS is wrong for anything you update or delete out of band — a write that lands in an old window breaks the one property you chose it for.

Repeating the demonstrations above takes about three minutes with compaction-strategies-guide.sh, which boots a single-node cluster twice, once on the stock configuration and once with default_compaction set, asserts the default strategy, the STCS merge, the LCS level layout, the TWCS windows and the UCS error messages, and leaves an installed Cassandra untouched; it needs Cassandra 5.x and its command-line tools on your PATH.

Compaction does not remove tombstones on demand

Running nodetool compact does not change that: inside gc_grace_seconds — 864000, ten days, by default — compaction is not permitted to drop a tombstone, so the marker survives the compaction you ran to remove it. Lowering the grace period to force the issue risks resurrecting deleted data. The demonstration, with sstabledump output either side and the reason the grace period exists at all, is on the tombstones page, which owns that topic.

Picking one

Pick TWCS if and only if the data is append-only and expires by TTL. Pick LCS if reads dominate and you can pay the write amplification — note that Cassandra also lowers bloom_filter_fp_chance from 0.01 to 0.1 for LCS tables, because LCS expects to touch fewer SSTables per read anyway, which is visible in a trace of the read path. Otherwise the honest answer on 5.0 is that STCS and UCS at T4 do close to the same thing, and UCS is the one still being developed. Switch with ALTER TABLE ... WITH compaction = {...} on a test table first: the change rewrites every SSTable for that table.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.