Explaining leveled vs tiered compaction
Leveled compaction organizes data into levels with non-overlapping key ranges per level, so reads check only one SSTable per level, resulting in low read amplification. However, data is rewritten multiple times as it moves through levels, causing high write amplification. Tiered (size-tiered) compaction merges runs of similar size less frequently, leading to fewer rewrites and lower write amplification, but overlapping SSTables within levels mean reads must check many files, increasing read amplification.
Log‑structured merge trees organize data as a sequence of immutable sorted “runs” or SSTables. You get fast, sequential writes by flushing new data to a new run, but over time you accumulate many runs. Compaction is the background process that merges and rewrites runs to bound the number of files you have to consult on a read, reclaim space from deleted/overwritten keys, and keep the total on–disk size under control.
Two common policies for deciding what to compact are:
-
Size‑tiered (tiered) compaction: You accumulate a handful of runs of approximately the same size at each “tier.” Once there are, say, four runs of size ~S, they are merged into a single run of size ~4 S, which may coexist with other runs of different ages and overlapping key ranges. The tree grows logarithmically in run size, but within a tier there can be many overlapping files.
-
Leveled compaction: The store is divided into levels with fixed capacities that grow by a factor (often 10) from one level to the next. Within a level, runs are kept non‑overlapping in key space. When a run in level i fills the level or exceeds overlap limits, it is merged with any overlapping runs in level i + 1 and rewritten into that lower level in a way that preserves disjointness.
These differing invariants explain the trade‑offs in amplification:
-
In a leveled scheme each key exists in at most one file per level, so a point lookup only has to check one run per level, and range scans can stream through a small number of non‑overlapping files. Read amplification is therefore low. The downside is that to maintain non‑overlap and the fixed per‑level size budget, data is rewritten as it cascades through each level; a given byte may be read and written several times as it moves from L0 to L1 to L2, etc. That increases write amplification.
-
In a size‑tiered scheme compaction is less aggressive: you only merge when you have collected enough similarly sized runs, and you don’t enforce disjointness within a tier. That means each byte is rewritten fewer times as it ages, so write amplification is lower. However, because there can be multiple overlapping runs at each tier, a read may have to probe many files to find the most recent version of a key. That leads to higher read amplification.
Systems such as RocksDB offer both strategies or hybrids so that you can choose the point on the read‑write amplification spectrum that suits your workload.