Sub topic 12
Compaction: merging files in the background
This chapter assumes you know how a memtable flushes into immutable SSTables, how a read checks the memtable and then SSTables from newest to oldest, and how tombstones mark deletes.
Why compaction is needed
Every flush adds one more SSTable. Without cleanup, three problems grow over time:
- A read may have to check many files, so lookups slow down.
- Overwritten values stay on disk next to their newer versions, so space is wasted.
- Tombstones and the values they delete both stay on disk.
Compaction is a background job that merges several SSTables into fewer, larger ones. It reads the inputs in sorted order and writes a new SSTable. For each key it keeps only the newest entry. After the new file is complete, the engine swaps it in and deletes the inputs.
A merge in code
The merge below takes tables ordered oldest first. Later tables overwrite earlier ones, so the newest value for each key wins. This code builds on the SSTable class and TOMBSTONE marker from the earlier chapter.
def compact(tables, drop_tombstones):
"""Merge SSTables (given oldest first) into one; newer values win."""
merged = {}
for table in tables:
for key, value in zip(table.keys, table.values):
merged[key] = value
items = sorted(merged.items())
if drop_tombstones:
items = [(k, v) for k, v in items if v is not TOMBSTONE]
return SSTable(items)
def compact_all(db):
if db.sstables:
db.sstables = [compact(db.sstables, drop_tombstones=True)]This version loads everything into a dictionary, which keeps the example short. A real engine streams the inputs with a merging iterator, so memory use stays small no matter how large the files are.
When tombstones can be dropped
A tombstone has a job: it hides older values of a key in older files. If you drop it too early, an older value can reappear.
Here is a failure case. Table A holds k = 1. Table B holds a tombstone for k. If you compact only B with some newer table and drop the tombstone, a read for k finds the old value in A again. The deleted key comes back.
compact_all is safe because the merge includes every SSTable. No older copy of any key can exist outside the merge, so the tombstones have nothing left to hide. Engines that merge only some files must keep a tombstone unless they can show that no older data lies below the output.
What compaction costs
Compaction trades background work for faster reads and less space. Each merge rereads and rewrites data that was already written, so one logical write can reach disk several times. This cost is called write amplification. Compaction also competes with foreground reads and writes for disk bandwidth. If flushes arrive faster than compaction can merge, files pile up and reads slow down.
The main design question is which files to merge and when. Size-tiered and leveled compaction answer it differently, and the next two chapters cover them.
Exercise
Table A (older) holds k = "x". Table B (newer) holds a tombstone for k. You compact only table B with a newer table C and drop tombstones. What does a read for k return afterward, and why?
Show answer
It returns "x". The tombstone was removed, but table A, which was not part of the merge, still holds the old value. The read finds it and the deleted key reappears. The tombstone can be dropped only when every older table is in the merge.