Chapter 08Down the Rabbit Hole: How Eagerly Should a Database Tidy Up?
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.
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
Truns and leaves them alone; atTruns it merges them all and pushes the result down.
LEVELING TIERING L0 = [1] L0 = [1] rewrote: 0 wrote: 1
Both just hold the first run.
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 amplification | Peak runs | |
|---|---|---|
| Leveling | 17.09 | 3 |
| Tiering | 4.00 | 27 |
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.
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.
L0 ▒▒ ▒▒ ▒▒ tiered (cheap writes)
L1 ▒▒▒▒ ▒▒▒▒ ▒▒▒▒ tiered
L2 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ tiered
L3 █████████████████████████████████████ leveled: ~90% of the data,
one run, low space ampDostoevsky's worst-case table:
| tiering | leveling | lazy leveling | |
|---|---|---|---|
| update | O(L/B) | O(L*T/B) | O((L+T)/B) |
| zero-result get | O(T*e^(-M/N)) | O(e^(-M/N)) | O(e^(-M/N)) |
| space amp | O(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.

Figure 1 from Liu, Xie and Luo, "ArceKV", arXiv:2508.03565, licensed CC BY 4.0. Cropped and recompressed.
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#
- O'Neil et al. The Log-Structured Merge-Tree (1996)
- Monkey (SIGMOD 2017)
- Dostoevsky (SIGMOD 2018)
- Dong et al. RocksDB paper (CIDR 2017)
- Facebook Engineering. MyRocks
- RocksDB wiki. Leveled Compaction, Universal Compaction, Write Stalls, Tuning Guide
- SILK (ATC 2019)
- Lethe (SIGMOD 2020) and Sarkar et al. compaction design space (VLDB 2021)
- ScyllaDB. Maximizing disk utilization with incremental compaction
- Apache Cassandra. Unified Compaction Strategy and tombstones
- CockroachDB. Technical Advisory 48860 and Pebble's range deletions doc
- CAMAL, EcoTune, ArceKV