Sub topic 13
Size-tiered compaction
This chapter assumes you know how a memtable is flushed into an SSTable, how a read checks tables from newest to oldest, and how tombstones mark deletes. If any of those are unclear, review the earlier chapters first.
The idea
Size-tiered compaction waits until several SSTables have about the same size. It then merges them into one larger table. Small tables merge into medium tables, medium tables into large ones, and so on. Each group of similar-sized tables is called a tier.
A common rule is to merge when a tier holds a fixed number of tables, often four. Each table is rewritten only when its tier fills up. A given key is therefore rewritten roughly once per tier it passes through, which keeps write cost low.
A tier-based trigger in code
This function groups tables by size and returns the first tier that is ready to merge. It uses the SSTable class from the earlier chapters and the compact function from the compaction chapter. Tables are grouped by the power of two nearest their entry count.
import bisect
TOMBSTONE = object()
class SSTable:
def __init__(self, items):
self.keys = [k for k, _ in items]
self.values = [v for _, v in items]
def compact(tables, drop_tombstones):
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 tier_of(table):
return max(len(table.keys), 1).bit_length()
def pick_tier(sstables, min_tables=4):
tiers = {}
for table in sstables: # oldest first
tiers.setdefault(tier_of(table), []).append(table)
for tier in sorted(tiers):
if len(tiers[tier]) >= min_tables:
return tiers[tier]
return None
tables = [SSTable([(i * 10 + j, "v") for j in range(4)]) for i in range(4)]
chosen = pick_tier(tables)
print(len(chosen), "tables chosen")
result = compact(chosen, drop_tombstones=False)
print(len(result.keys), "keys in merged table")Note the drop_tombstones=False argument. The tables in one tier are usually not the oldest tables, so an older table outside the merge may still hold a value for a deleted key. Dropping the tombstone would bring that old value back. A tombstone can be dropped only when the merge includes every older table that could contain the key.
Trade-offs
Size-tiered compaction favors writes, but it costs reads and space.
| Property | Behavior |
|---|---|
| Write amplification | Low. Each byte is rewritten about once per tier. |
| Read amplification | Higher. Many tables can overlap in key range, so a lookup may check several. Bloom filters reduce this. |
| Space amplification | Higher. Old versions and tombstones stay until a merge covers them. |
The space cost has a sharp edge. Merging the largest tier needs room for both the inputs and the output, so you can briefly need close to double the data size on disk.
Size-tiered compaction suits write-heavy workloads such as logging and time-series ingestion. The next chapter covers leveled compaction, which trades extra write work for better reads and less wasted space.
Exercise
A size-tiered engine merges four tables from the smallest tier. One of them contains a tombstone for key "x". Older, larger tables exist that were not part of the merge. Can the merge drop the tombstone? Why?
Show answer
No. A larger, older table may still contain a value for "x". If the tombstone were dropped, a read would fall through to that old value and the deleted key would reappear. The tombstone can be dropped only when the merge includes every older table that might hold the key.