jonnify·persistence /echo-persistence/cubdb/compaction

Module 4 · Dive 4.3

Compaction.

Never overwriting has a price: the file only grows. Every update supersedes nodes that are now dead — still on disk, no longer reachable from the current root. Compaction is the sweep: copy the live data to a fresh file and reclaim the rest. Write a few times to pile up dead blocks, then compact.

§1

Live versus dead

interactive
The file only grows. Live blocks are reachable from the current root; dead blocks are superseded older versions. Write to accumulate dead; compact to copy the live set to a fresh file.
the one file: live fraction of the file: file: 4 blocks · 4 live · 0 dead
readout · Write a few times: each update appends new live blocks and turns superseded ones dead — the file grows, the live set stays small. Compact to copy the live blocks to a fresh file and reclaim the dead space.
§2

A background sweep, not a stall

store.design.md

Compaction reads the live data — everything reachable from the current root — and writes it to a new file, then switches over and drops the old one. It runs in the background while normal reads and writes continue against the live tree, because immutability means the data being copied never changes underfoot. The trigger is a ratio, not a clock: when dead data grows past a threshold of the file, sweep. For the durable tier this keeps the page log’s footprint bounded without ever blocking an enqueue, and it reuses the same “copy the live set forward” move that recovery and replication lean on elsewhere.

§3

References & sources