Sub topic 15
Write, Read, and Space Amplification
This chapter assumes you know how a memtable, SSTables, Bloom filters, and compaction work. It gives you three ratios for comparing compaction strategies and tuning choices.
The three amplifications
Amplification is the ratio between the work the engine does and the work the application asked for.
- Write amplification is bytes written to disk divided by bytes the application wrote. One byte of user data may be written to the log, to a flushed SSTable, and again by every compaction that rewrites it.
- Read amplification is the number of disk reads, or bytes read, per lookup. A point lookup may check the memtable and several SSTables before it finds the key or proves it is missing.
- Space amplification is bytes on disk divided by the bytes of live data. Overwritten values, tombstones, and files waiting to be compacted all take space.
You cannot minimize all three at once. Compaction effort moves cost from one ratio to another.
How compaction strategy moves the numbers
Size-tiered compaction waits until several files of similar size exist, then merges them. Each byte is rewritten only a few times, so write amplification is low. Many overlapping files can exist at once, so a lookup may touch many of them. The same key can also live in several files, and a merge needs room for both its inputs and its output, so space amplification is high.
Leveled compaction keeps each level as non-overlapping files, with each level about ten times larger than the one above. A lookup needs at most one file per level, so read amplification is low. Overlap is small, so space amplification is low. A byte may be rewritten many times as it moves down the levels, so write amplification is high.
| Strategy | Write amplification | Read amplification | Space amplification |
|---|---|---|---|
| Size-tiered | Low | Higher | Higher |
| Leveled | Higher | Lower | Lower |
Measuring them in TinyLSM
You can estimate write and space amplification in the tiny store from earlier chapters by counting entries instead of bytes.
def space_amplification(db):
stored = sum(len(t.keys) for t in db.sstables)
live = {}
for table in db.sstables: # oldest first, newest wins
for k, v in zip(table.keys, table.values):
live[k] = v
live_count = sum(1 for v in live.values() if v is not TOMBSTONE)
return stored / live_count if live_count else float("inf")If you overwrite the same four keys many times, each flush adds a table with the same keys. The ratio grows with each flush. After compact_all(db), the ratio falls back to 1.0.
Common errors
- Reporting only write amplification. A strategy that looks cheap to write can make reads or disk usage unacceptable.
- Forgetting temporary space. A compaction needs room for its output while the inputs still exist, so a disk that is nearly full can stall it.
- Ignoring Bloom filters when estimating read amplification. They remove most reads for absent keys, but not for keys that exist.
Exercise
A workload overwrites a small set of hot keys at a very high rate and is rarely read. Which compaction strategy fits it better, and which amplification do you accept?
Show answer
Size-tiered compaction fits better. Reads are rare, so higher read amplification costs little, and low write amplification keeps up with the heavy write rate. You accept higher space amplification, because old versions of the hot keys remain in several files until a merge removes them.
Exercise
An engine wrote 50 GB for the application, and its disk activity shows 400 GB written in total, including flushes and compactions. What is the write amplification?
Show answer
400 GB / 50 GB = 8. Each application byte was written to disk about eight times.