← Back to all chapters

Chapter 08Down the Rabbit Hole: How Eagerly Should a Database Tidy Up?

7 min read
rabbit-holedatabasesstoragedata-structures

Facebook's MyRocks post has a number I didn't believe. Moving a MySQL workload from InnoDB (B-tree) to RocksDB (LSM-tree) made one instance 3.5x smaller than uncompressed InnoDB and 2x smaller than compressed InnoDB, with "orders of magnitude less" data written. Whatever an LSM-tree does, it does in the background, and it has a name: compaction. I came out with a dial, an equation for why the dial exists, and a trick for having it both ways.

The librarian's cart#

A librarian gets new books all day. Shelving each in its exact alphabetical spot means a walk across the library every time, so new books go on a cart, which is sorted once when full and shelved as a batch.

That's the LSM-tree. Writes land in an in-memory sorted memtable (plus a write-ahead log). When it fills, it is flushed as one immutable sorted file, a run. One sequential write instead of a random one per update: the whole write-side win over a B-tree.

The catch is reads. A key's latest version could be in the memtable or any run, and overwritten or deleted data never goes away. So the librarian periodically merges batches into bigger sorted ones. Merge constantly and there are few batches to search but the same books get re-handled. Merge lazily and shelving is cheap but searches check more batches.

writesmemtablesorted, in memorywrite-ahead logflushrunrunrunrunimmutable sorted filesmergecompaction
The LSM write path. Run counts and sizes are illustrative.

Leveling and tiering#

Data is organised into levels, each roughly T times bigger than the one above (T is the size ratio; RocksDB's default is 10).

  • Leveling: each level holds one sorted run. Anything arriving is merged into it immediately.
  • Tiering: a level collects up to T runs and leaves them alone; at T runs it merges them all and pushes the result down.
1/4flush 1
LEVELING            TIERING
L0 = [1]            L0 = [1]
rewrote: 0          wrote: 1

Both just hold the first run.

Leveling vs tiering with T = 3, one unit per flush. Step through.

Under leveling, data already in a level is rewritten every time new data arrives. Under tiering each entry is written roughly once per level, but a lookup may check up to T - 1 runs per level, where leveling has at most one.

The equation#

The Monkey paper (SIGMOD 2017) gives update cost in page I/Os per entry. L is levels, B entries per page, φ the write cost relative to a read.

leveling: W = (L / B) * ((T - 1) / 2) * (1 + φ)
tiering:  W = (L / B) * ((T - 1) / T) * (1 + φ)

Only the middle factor differs. Under leveling an entry is rewritten about (T - 1) / 2 times before its level overflows; under tiering about once per level. At T = 10: 4.5 rewrites per level versus 0.9. Lookups go the other way: with per-run Bloom filters, a missing-key lookup costs the sum of false positive rates under leveling and T - 1 times that under tiering.

The dive's small simulator agrees on ordering. At T = 10, three levels:

Write amplificationPeak runs
Leveling17.093
Tiering4.0027

It counts the initial flush and boundary effects the asymptotic formula drops, so absolute numbers sit above theory, but the ratios match. RocksDB's leveled compaction wiki calls write amplification "often larger than 10", and its tuning guide works an example out to 33.

  • Leveling17.09
  • Tiering4.00
Write amplification in the dive's simulator, T = 10, three levels.

The trick: treat the last level differently#

Levels grow geometrically, so the last holds about (T - 1) / T of the data: 90% at T = 10. Space amplification and lookup cost are decided almost entirely by the last level. Update cost piles up across every level. Different levels, so pick different policies.

last level: ~90% of the dataL1L2Space and lookup cost: set by the last level. Update cost: summed over all levels.L3
Why the last level dominates: each level is 10x the one above (T = 10, three levels). Proportions are schematic.
 L0   ▒▒ ▒▒ ▒▒            tiered   (cheap writes)
 L1   ▒▒▒▒ ▒▒▒▒ ▒▒▒▒      tiered
 L2   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    tiered
 L3   █████████████████████████████████████   leveled: ~90% of the data,
                                              one run, low space amp
Lazy leveling (Dostoevsky, SIGMOD 2018): tier the top, level the bottom.

Dostoevsky's worst-case table:

tieringlevelinglazy leveling
updateO(L/B)O(L*T/B)O((L+T)/B)
zero-result getO(T*e^(-M/N))O(e^(-M/N))O(e^(-M/N))
space ampO(T)O(1/T)O(1/T)

Lazy leveling keeps leveling's lookup and space costs and cuts update cost from L * T to L + T. It only hurts short range scans, which see more runs in the upper levels. The paper generalises this into a "Fluid LSM-tree" with two knobs (runs allowed in upper levels, runs allowed in the last), so the three policies are corners of one space.

The same idea shows up in practice. RocksDB's level_compaction_dynamic_level_bytes (default since 8.4) sizes levels bottom-up so the last holds about 90% of the data, with space amplification around 1.11 at T = 10. Cassandra 5.0's Unified Compaction Strategy makes the whole leveled-to-tiered dial one scaling parameter.

Monkey's Bloom filter allocation

Give every run the same bits per entry and lookup cost grows with the number of levels. But probing any run costs one I/O regardless of its size, so memory buys more on small levels. Allocating so each level's false positive rate is T times the one above makes lookup cost independent of the number of levels.

Where it breaks#

The formulas are worst-case bounds in a model that only counts page I/Os. Real systems break where the model doesn't look.

ArceKV Figure 1: three panels showing a point lookup skipping runs whose Bloom filter answers negative, a range lookup merging sorted runs and discarding duplicates, and a put that flushes the memtable, compacts a full level into the next, and stalls the put if a condition is met.

Figure 1 from Liu, Xie and Luo, "ArceKV", arXiv:2508.03565, licensed CC BY 4.0. Cropped and recompressed.

Four ways compaction bites. Click a box.
  • Before incrementalabout 200 GB
  • Incremental (about 1 GB fragments)about 2 GB
ScyllaDB incremental compaction: temporary space during a big compaction (GB).

The question I'm left with#

Sarkar et al. (VLDB 2021) broke compaction into four primitives and benchmarked ten strategies. My reading, not a quote: none wins on every metric. So if the workload changes, can you change the policy?

That's the frontier. CAMAL uses active learning to tune RocksDB, reporting 28% on average and up to 8x over previous configurations. EcoTune treats compaction as an investment in future query throughput and solves it with dynamic programming. ArceKV makes compactions and stalls actions picked by a lightweight decision engine.

What keeps it hard: every layout change is paid for in rewritten data. Switching from tiering to leveling means re-merging terabytes. The cost of the transition is rarely in the model, and workloads drift faster than you can amortise it. I don't think anyone has an optimal online scheduler that accounts for that, and I'm not sure what one would look like on object storage, where every request has a price.

Sources#

© 2026 Bharat Kaurav. Built with Next.js & Tailwind.

>_ bk@netherwatcheresc to close

Welcome, reader. Type help to see what this shell can do.