Skip to main content

Leveled vs Tiered Compaction: Choosing Which Bill Your LSM Tree Pays

Every LSM tree pays for writes, reads and disk space. Compaction decides which bill gets big. Measured on a simulator, then mapped to RocksDB and Cassandra settings.

Updated 13 min readMarkdown
Part of seriesLSM Trees from First Principles
Leveled vs Tiered Compaction: Choosing Which Bill Your LSM Tree Pays

Picture a feature store for a recommendations system. A batch job recomputes every user's features a few times a day and writes them back to Cassandra. Same keys, new values, over and over.

Live data is 400 GB per node, on a 1.6 TB disk. The dashboard has shown the disk at about 55% for weeks. Then one night a node runs out of disk in the middle of a compaction, and nothing about the traffic changed.

Nobody misconfigured anything. That's what size-tiered compaction does to a workload that overwrites its keys, and it's the default Cassandra gives a table if you don't pick a strategy.

In Part 1 we saw that compaction is what keeps an LSM tree readable. This article is about how you compact, because that one choice decides where your costs land. We'll look at the two classic strategies, measure them on the same workloads, and then look at the hybrid that most modern engines are moving to.

Everything here is backed by one Python script you can run (link at the end).


1. Three bills, and you only get to shrink two

Every LSM tree pays three costs, and compaction decides how big each one is:

  • Write cost (write amplification): how many times each byte is rewritten on its way down the tree. This is disk bandwidth, and on SSDs it's also drive wear.
  • Read cost: how many sorted runs a lookup or a range scan has to check. Bloom filters (Part 2) make point lookups cheap, but a range scan still has to open every run.
  • Space cost (space amplification): disk used divided by live data. Old versions of a key and deleted keys sit on disk until compaction reaches them.

Merging eagerly keeps reads and space small, but rewrites data more often. Merging lazily saves writes, but leaves more runs and more stale copies around. Researchers call this the RUM conjecture (Read, Update, Memory): you can make two of the three small, not all three.

The two classic strategies sit at opposite ends.


2. Leveled: merge early, keep it tidy

This is what RocksDB and LevelDB do by default, and what we built in Part 1.

L0   [f][f][f][f]                       ← fresh flushes, overlapping
L1   [ a–f ][ g–m ][ n–s ][ t–z ]       ← one sorted run, split into files
L2   [a–b][c–d][e–f] ... [y–z]          ← 10× bigger, still one sorted run
L3   ...                                ← 10× bigger again

The rule: every level below L0 is one sorted run. No two files in a level overlap.

When a level gets bigger than its target, the engine picks one file from it and merges that file into the files it overlaps in the next level. Because the next level is 10× bigger, one file's key range overlaps about 10 files there. So pushing one file down means rewriting about 11 files' worth of data.

That's the write cost: roughly up to 10 rewrites per level, for every byte, at every level it passes through.

What you get in return:

  • Reads are cheap. One file per level, so a lookup checks L0 plus one file per level.
  • Space is cheap. About 90% of the data sits in the last level. Even if every key in the upper levels were a stale copy, that's only about 11% extra.
  • Each compaction is small. One file plus its neighbours, not a whole level. You never need much free disk to run one.

3. Tiered: merge late, write less

This is Cassandra's size-tiered strategy (STCS) and RocksDB's universal compaction.

small    [r][r][r][r]        ← 4 similar-sized runs? merge them into one
medium   [ run ][ run ]      ← wait for 4 of these
large    [   run   ]

The rule: wait until you have several runs of similar size (4 by default in Cassandra), then merge them all into one bigger run. Never merge a small run into a big one.

Each byte gets rewritten once per size tier, not ~10 times per level. That's why tiered compaction is the write-friendly choice.

What it costs:

  • More runs to check. Each tier can hold several runs, so a lookup or scan may touch many more of them.
  • Stale copies pile up. If a key is updated often, a copy of it lives in every run that got written since the last merge. Four big runs can mean four copies of most of your data.
  • Big merges need big free space. The merged output has to be fully written before the inputs are deleted. When the biggest tier merges, you briefly need room for the inputs and the output.

4. Measured side by side

I wrote a simulator that runs real sorted runs through each strategy: RocksDB-style leveled (L0 trigger of 4, levels 10× apart) and Cassandra-style size-tiered (merge 4 similar-sized runs). Same memtable size, same stream of writes.

Workload 1: insert-only (4M new keys, like an event log)

Leveled Tiered
Bytes written per byte inserted 12.8× 4.4×
Runs a lookup checks (average / worst) 3.5 / 6 6.3 / 12
Disk used / live data 1.00× 1.00×
Biggest single compaction (% of live data) 12% 100%

Tiered writes about 3× less. With no overwrites, there are no stale copies, so space looks the same. But notice the last row: at some point tiered compaction merges all of your data at once, so for a while it needs double the disk. That's where the well-known Cassandra and ScyllaDB rule of thumb comes from: keep about 50% of the disk free with size-tiered compaction.

Workload 2: update-heavy (4M writes over 400K keys, like the feature store)

Leveled Tiered
Bytes written per byte inserted 7.6× 3.5×
Runs a lookup checks (average / worst) 3.3 / 5 6.1 / 12
Disk used / live data (average) 1.12× 2.18×
Peak disk during compaction / live data 1.34× 4.30×

(Write cost is lower than in workload 1 for both, because overwritten versions get dropped during merges instead of being carried down.)

This is the feature store from the intro. On average, tiered compaction keeps 2.2× the live data on disk: 870 GB of a 1.6 TB disk, the "55%" on the dashboard. But every so often it merges four big runs that each hold a copy of nearly every key. For that merge it needs 4.3× the live data, about 1.7 TB. The disk is 1.6 TB.

The "50% free" rule doesn't save you here. It assumes the biggest merge is about the size of your data. With heavy overwrites, it can be several times that.

Leveled compaction pays 2× more writes, but its disk use barely moves.

Leveled pays in writes. Tiered pays in disk space, and in big, sudden bursts of it.


5. The hybrid: tiered on top, leveled at the bottom

Two facts point to a better design:

  1. Writes cost about the same at every level. Each level rewrites each byte a similar number of times, whatever its size.
  2. Space and reads are decided at the bottom. The last level holds about 90% of the data, so that's where stale copies and lookups pile up.

So use tiered merging for the small upper levels, where it saves writes, and keep the last level as a single sorted run, where it keeps space and reads under control. This is called lazy leveling (from the Dostoevsky paper, Dayan and Idreos, SIGMOD 2018).

Update-heavy workload Leveled Tiered Hybrid
Bytes written per byte inserted 7.6× 3.5× 5.4×
Runs a lookup checks (average) 3.3 6.1 3.7
Disk used / live data (average) 1.12× 2.18× 1.15×

On insert-only, the hybrid wrote 7.7× vs 12.8× for leveled. So it keeps much of tiered's write saving while keeping almost all of leveled's space efficiency.

One caveat from the simulator: my hybrid merges into the last level in one go, so its biggest merge is still large. Production versions avoid that by doing the bottom merge in pieces, a few files at a time, and freeing inputs as they go. That's exactly what Cassandra's and ScyllaDB's newer strategies do (section 7).


6. A special case: data that expires

Metrics, logs and IoT readings have a shape no general strategy handles well: data arrives in time order, is almost never updated, and gets deleted by age (a TTL).

Merging that data is pure waste. You rewrite rows that will be deleted in a few days anyway.

The fix is to not merge across time at all:

  • Cassandra's TimeWindowCompactionStrategy (TWCS) groups SSTables into time windows (say, one per day). Inside the current window it compacts normally; once the window closes, it compacts it down to one file and never touches it again. When every row in a file has expired, the whole file is dropped. No merge, no rewrite.
  • RocksDB's FIFO compaction goes further: it just deletes the oldest files once the total size passes a limit.

One warning from production: TWCS assumes data really is written in time order. If an application updates or deletes old rows, those writes land in today's window while the originals sit in an old one, and the old files can no longer be dropped cleanly.


7. What production engines actually do

RocksDB

  • Leveled is the default, and it's already a small hybrid: L0 is tiered. Flushed files pile up until level0_file_num_compaction_trigger (4) of them exist, then they merge into the level below together.
  • level_compaction_dynamic_level_bytes is now on by default. It sizes levels from the bottom up, so the last level always holds about 90% of the data. With it turned off, my simulator's update-heavy run used 1.75× the live data instead of 1.12×, because the database's size didn't line up with the fixed level targets and a "middle" level ended up holding stale copies of most keys.
  • Universal compaction is RocksDB's tiered strategy. Its max_size_amplification_percent defaults to 200, which means the database can grow to 3× the live data before a full merge is forced, and that full merge temporarily needs double the disk again.
  • Write stalls are how leveled compaction tells you it's falling behind. RocksDB slows writes at 20 L0 files and stops them at 36, and does the same when pending compaction bytes pass 64 GB (slow) and 256 GB (stop). If you see stalls, compaction can't keep up with your write rate.

Cassandra and ScyllaDB

  • STCS is still the default if a table doesn't specify a strategy. It merges once 4 similar-sized SSTables exist.
  • LCS (leveled, 160 MB SSTables, levels 10× apart) is the recommended choice for read-heavy tables and tables with lots of updates and deletes. The feature store belongs here.
  • TWCS for time series and TTL data, as above.
  • UCS (Unified Compaction Strategy, Cassandra 5.0) is now the recommended strategy for new tables. It mixes tiered and leveled behaviour, splits data into shards so merges are smaller and run in parallel, and can switch between tiered-like and leveled-like settings without a full recompaction. Changing strategy used to mean rewriting the whole table.
  • ScyllaDB's Incremental Compaction (ICS) keeps size-tiered's low write cost, but merges big runs fragment by fragment and frees space as it goes. ScyllaDB's rule of thumb drops from 50% free disk to about 30%.

Watch it in production. For leveled: L0 file count, pending compaction bytes and write stall time. For tiered: disk free compared to the size of your largest SSTables, not just the percentage used. In Cassandra, nodetool compactionstats shows pending compactions and nodetool tablestats shows SSTable counts per table.


8. How to choose

Your workload Pick Why
Read-heavy, or keys updated and deleted often (profiles, carts, feature stores) Leveled Low space overhead, few runs per read, small compactions
Write-heavy, mostly new keys, disk to spare (event ingestion, audit logs) Tiered Lowest write cost; keep plenty of disk free
Heavy writes and you care about disk Hybrid (UCS, ICS, lazy leveling) Most of tiered's write saving, most of leveled's space efficiency
Time-ordered data that expires TWCS / FIFO Drop whole files instead of merging

If you're unsure, start leveled. Its failure mode is slow: write amplification and stalls that show up on dashboards. Tiered's failure mode is sudden: a disk that fills up in one night.


The short version

  1. Compaction decides how big three bills get: writes, reads and disk space. You can keep two of them small, not all three.
  2. Leveled keeps one sorted run per level. It rewrites data more (12.8× in our insert test), but keeps reads and space tight and every compaction small.
  3. Tiered merges runs of similar size. It writes about 3× less, but leaves more runs to read and can need several times your live data in disk space when keys are overwritten (4.3× at peak in our update test).
  4. Hybrids use tiered merging on top and a single sorted run at the bottom. They keep most of tiered's write savings with leveled's space efficiency, and modern engines (Cassandra UCS, ScyllaDB ICS) do the big merges in pieces.
  5. For data that expires by time, don't merge at all: TWCS or FIFO drop whole files.

What's next

This is part 3 of LSM trees from first principles. Next up: deletes. In an LSM tree, a delete is just another write, and the tombstones it leaves behind can quietly slow down reads and keep disk space from coming back.

Code for this article:

  • compaction_sim.py: leveled (with and without dynamic level sizing), size-tiered and lazy leveling on insert-only and update-heavy workloads

Further reading

  • Athanassoulis et al., Designing Access Methods: The RUM Conjecture, EDBT 2016
  • Dayan, Idreos, Dostoevsky: Better Space-Time Trade-Offs for LSM-Tree Based Key-Value Stores, SIGMOD 2018
  • RocksDB wiki, Leveled Compaction, Universal Compaction and Write Stalls
  • Apache Cassandra docs, Compaction overview and Unified Compaction Strategy
  • ScyllaDB blog, Maximizing Disk Utilization with Incremental Compaction, 2020

About the author

  • Hi, I'm Kartik, a software engineer based in Pune. By day I work on high-traffic services and infrastructure. In my spare time I like taking systems apart to see how they work. I write about what I learn along the way, from distributed systems to the bugs that taught me the most. Cheers !

No comments yet. Be the first to comment.

LSM Trees from First Principles

Part 3 of 3

How log-structured merge trees work, derived from disk physics up: why they exist, Bloom filters, compaction, and the trade-offs behind RocksDB, Cassandra and friends. Every number is backed by a simulation you can run.

Start from the beginningWhy LSM Trees Exist: The Disk Math Behind Every Write-Heavy DatabaseDeriving the log-structured merge tree from one constraint: small random writes are expensive.