Skip to content
WebsiteHosting.com

concept Data & storage

LSM-tree compaction and write amplification

Compaction can shrink a set of sorted runs while increasing total bytes written. Follow a four-run merge to separate the cost of rewriting entries from the benefit of having fewer places to look.

Latest recorded source check · P0143

What it means

In a log-structured merge-tree (LSM-tree), compaction merges sorted runs so a lookup has fewer places to search. A run is a collection of entries sorted by key. Several runs can cover the same keys, so sorting within each run does not eliminate the need to check other runs. A merge can remove obsolete entries, but it must write the entries it keeps. That is how compaction can leave less data in the result while increasing the total bytes written.

Try it

Simulation. Start with four runs of 100 entries each, at 100 bytes per entry. The initial policy, merge-all, leaves one candidate run for a lookup and counts 74,000 bytes written. Change only Compaction policy to defer. The write count falls to 40,000 bytes, but all four runs remain candidates. Deferring avoids a rewrite now; it does not undo the initial writes.

LSM-tree compaction and write amplification

Explain how sorted runs and compaction trade write work for reads

Simulation
Modeled outcome: Merged

A modeled full merge rewrites remaining entries; only explicitly safe tombstones are removed.

LSM-tree compaction and write amplification: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Run merge and write ledger

Runs and retention

  1. Input runs4
  2. Entries per run100
  3. Policymerge-all
  4. Tombstone safetyno
  5. Remaining entries340
  6. Run probes1
Modeled entry writes74000 bytes
Initial writes: 40000 bytesMerge rewrite: 34000 bytes
Logical input40000 bytes
Total physical writes74000 bytes

The entry counts use a fixed record size. Deferral does not evaluate tombstone removal. A merge only removes the stipulated obsolete entries and tombstones whose safety is established.

Evaluated inputs

Required scenario evidence
provided
Sorted runs
4 count
Entries per run
100 count
Proven obsolete entries
60 count
Tombstone entries
20 count
Equal bytes per entry
100 B
Compaction policy
merge-all
Evidence tombstones can be discarded
no

Equal-size encoded records, disjoint obsolete/tombstone counts stipulated; no older overlapping levels beyond fixture, compression, WAL, filesystem writes, caches or snapshot implementation. Physical writes count only initial run writes and this one full merge, not device-level write amplification. Run probes are candidate runs, not measured disk reads.

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

Use Reset to restore the starting example. This time, leave the policy at merge-all and change Evidence tombstones can be discarded from no to yes. A tombstone is an entry that records a deletion. Watch the merge-write bytes, not just the run count: the result is still one run, but the merge writes 2,000 fewer bytes because it no longer copies the 20 tombstones.

Where the extra writes come from

The input contains 400 entries. Of those, 60 are proven obsolete and a separate 20 are tombstones. With tombstone removal set to no, the merge skips the 60 obsolete entries and writes the other 340 into the new run. The 20 tombstones are included in that 340, not added to it.

What is countedDeferMerge, keep tombstones
Logical input40,000 B40,000 B
Initial run writes40,000 B40,000 B
Merge writes0 B34,000 B
Total run writes40,000 B74,000 B
Entries remaining400340
Candidate runs for a lookup41

The first two rows describe the same input from different perspectives: 40,000 bytes of entries arrived, and writing their initial runs cost 40,000 bytes. Do not add those two rows. Add initial run writes + merge writes. The 34,000-byte merged run is smaller than the input, but its bytes are writes in addition to the original 40,000. Dividing 74,000 by 40,000 gives 1.85 bytes written per logical input byte in this example.

Why a deletion marker can survive a merge

Suppose an older run contains a value for key a, and a newer run contains a tombstone for a. If the marker disappears while the old value is still visible to a lookup, the deleted value can appear again. Compaction must also preserve what a retained snapshot, a view of the data at an earlier point, can see.

The simulation takes removal safety as an input rather than trying to prove it from the number of runs. With yes, it drops both the 60 obsolete entries and the 20 markers. That leaves 320 entries to rewrite: 32,000 bytes for the merge and 72,000 bytes written in total. With no, it has a definite instruction to retain the markers. With unknown, it has no definite removal decision and reports no merge result. An unknown result is not a merge that cost zero bytes.

The write ledger separates initial writes from merge rewrites. In the deferred case the rewrite count is zero because no merge occurs in the modeled interval, not because obsolete entries were removed for free. Compare the retained-entry count and run-probe count before interpreting that smaller write total.

In practice

When interpreting a compaction statistic, first ask whether it counts bytes in the resulting runs, bytes written by the merge, or all run writes over the period of interest. Here, output size and merge-write bytes coincide because the model writes one full merge of equal-size entries. It does not count compression, the write-ahead log, filesystem writes, or writes inside the storage device. Its 1.85 ratio is not a prediction for a particular engine or workload.

Read the lookup count just as narrowly. One candidate run means one sorted collection that might need inspection, not one disk read. Filters, indexes, and cached blocks affect the work of an actual lookup. Nor does this example model older overlapping levels outside the four runs or implement snapshot visibility checks.

The useful comparison is which rewrite buys which reduction in candidate runs. Here, rewriting 34,000 bytes takes the lookup from four candidates to one. Removing the tombstones would save another 2,000 write bytes but would not reduce that count further. Whether those bytes can be saved is a correctness question: establish what must remain visible before counting a deletion marker as reclaimable space.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

When can tombstones disappear?

In this lab a tombstone may disappear only when supplied evidence says dropping it cannot expose an older deleted value or change a retained snapshot. RocksDB compaction takes snapshot visibility into account. Do not treat the lab as its tombstone-removal algorithm.

Does one merged run mean one disk read?

No. A run can contain many blocks; filters, indexes and cache hits influence the actual work. The lab counts candidate run probes rather than measured disk reads.

How do you calculate write amplification here?

For this closed example divide total run-write bytes by initial logical input bytes. State that WAL, compression and device-level writes are excluded before comparing a real metric.

Do all compaction policies have the same tradeoff?

No. Deferring a merge can avoid immediate rewrites while leaving more entries and runs to inspect. Merging can reduce those counts at a write cost. The real RocksDB policy and workload determine the actual tradeoff; the lab is one full-merge fixture.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. RocksDB compaction

    Compaction. Leveled merges can rewrite overlapping runs; tiered merges runs with different read/write/space tradeoffs. No fixed amplification applies to all workloads.

    Checked .

  2. RocksDB Snapshot

    Implementation: flush and compaction. Flush and compaction preserve applicable retained-snapshot visibility. The lab stipulates removal evidence rather than implementing a RocksDB compaction algorithm.

    Checked .

  3. RocksDB overview

    Prefix Bloom filters; block cache. Filters, indexes and cache hits influence the work of reading a run. Candidate run probes in the model are not measured disk reads.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.