What does compacting a write log replace it with, what triggers it, and what does the compaction itself cost while the store serves?
answer
- the file grows with writes, not data
- history, not live values
- a shorter log, same rebuilt state
- the rewrite itself costs while serving
basics
~20 sCompacting replaces an accumulated write log with a shorter one that reconstructs the same state, dropping history later overwritten or removed. Growth thresholds, a schedule or an operator trigger it, and it costs disk, memory and a latency bump.
solid answer
~50 sA write log records history, so it grows with write volume rather than with the size of the keyspace: one counter incremented a million times is a million entries and one live value. Compaction is the repair - replacing the accumulated log with a shorter one that reconstructs the same state. How that shorter log is produced varies: some stores walk the live keyspace and emit a minimal set of operations that recreate it, others merge the existing log, others write a compact representation of the keyspace instead. Triggers vary too - a growth ratio, an absolute size, a schedule, or an operator asking for it. It is not free: writes keep arriving while it runs, so the store buffers or double-writes that delta, and the work competes with serving for processor time and disk bandwidth.
go deeper
Remember the shape: because the log records every change, it keeps growing even when the amount of live data does not, and compaction is the step that replaces it with a shorter file rebuilding the same state.
Explain why log size follows write volume rather than keyspace size, define compaction as producing a shorter log that reconstructs the same state, and name the trigger classes and the costs it incurs while the store keeps serving.
Show the operational side: the delta of writes arriving during the rewrite, memory and disk headroom, the latency bump for concurrent callers, and the failure mode where compaction cannot outrun the incoming write rate.
Connect compaction to recovery time rather than to disk usage, and treat a log that must be compacted constantly as evidence that this posture is mispriced for the write rate the tier is being asked to absorb.
## Why the log outgrows the data it describes The write log is a history of operations, not a picture of the keyspace. That has a direct consequence: **its size tracks write volume since it was last shortened, not how much data is live.** A single counter incremented a million times contributes a million entries and one live value. Entries whose keys were later removed are still in the file, as is every superseded value. A tier holding a modest working set can therefore carry a log many times larger than the keyspace it would rebuild. Two costs grow with it: - **Disk.** The file grows monotonically as long as writes arrive. - **Recovery time.** Replay at start applies every entry in the log, so a long history means a long start, even though the keyspace it ends up with is small. ## What compaction produces **Compacting the write log means replacing an accumulated log with a shorter one that reconstructs the same state.** The end state is identical; what disappears is the history of how it was reached. How stores produce that shorter file differs: - Some walk the live keyspace and emit a minimal set of operations that would recreate it from empty. - Some merge the existing log, collapsing repeated writes to the same key. - Some write a compact representation of the keyspace itself and then continue appending new operations after it. The floor is set by the live keyspace: a compacted log has to describe every value that is still there, so it cannot be smaller than that, however much history it discards. ## What triggers it Stores differ here, and the honest answer names the class of trigger rather than a number: - **A growth ratio** - the log has grown by some multiple of its size after the previous compaction. - **An absolute size** - the file crosses a configured threshold. - **A schedule**, run during a window with headroom. - **An operator asking for it**, typically before or after something else expensive. - **Continuous background maintenance**, in stores that fold compaction into ordinary housekeeping rather than treating it as an event. In several stores this is automatic, with a knob rather than a command; assuming an operator must trigger it is as wrong as assuming it never needs attention. ## What it costs while the store is serving Compaction runs against a process that is still accepting writes, which is the whole source of its cost. | Cost | What drives it | How it shows up | |---|---|---| | Disk | The live keyspace, plus writes arriving during the rewrite | Two logs on disk at once until the switch-over | | Memory | The delta of writes arriving while it runs - write rate times duration | Resident size rises for the duration, sometimes sharply | | Processor and disk bandwidth | Keyspace size and device throughput | A latency bump for concurrent callers | | A pause | The switch-over, and in some designs the buffered delta being applied | A brief stall rather than a sustained one | The writes arriving during the rewrite have to end up in the new log. The store either buffers them and appends them after the switch-over, or writes them to both files as it goes. Either way the delta is proportional to **write rate times how long the compaction takes**, which is why the same compaction is cheap at night and expensive at peak. One failure mode is worth naming: if compaction cannot finish faster than writes accumulate, the log grows anyway and the work has been spent for nothing. That is a signal the tier is being asked to absorb a write rate this posture cannot pay for. ## Operating around it 1. **Leave headroom in memory** for the delta, not just for the keyspace, and leave disk for two logs at once. 2. **Do not set the trigger so tight** that compaction runs continuously; a log that is allowed to grow somewhat before being shortened costs less overall than one rewritten constantly. 3. **Watch when it overlaps other expensive work** - a compaction competing with a traffic peak is a latency incident with an obvious cause and a non-obvious signature. 4. **Treat it as the lever on recovery time**, not only on disk usage. The reason to keep the log short is usually how long a start would take, and that connection is what a weak answer misses.
- What sets the floor on how small a compacted write log can be?The live keyspace. The new log still has to describe every value that is still there, so its size tracks the data rather than the history. What compaction removes is superseded values and entries for keys that no longer exist, which is why the saving is large on a write-heavy keyspace of stable size and small on one that only grows.
- Where do writes that arrive during a compaction end up?In the new log, one way or another. Stores either buffer them and append them after the switch-over, or write them to both files while the rewrite runs. The size of that delta is the write rate multiplied by how long the compaction takes, which is what turns a routine rewrite into a memory spike at peak traffic.
- Besides disk usage, why does compaction matter?Because replay at start applies every entry in the log, the length of the log is the length of a restart. Compaction is therefore the main lever on recovery time: shortening the history shortens the start. A team that only watches disk usage will happily run a long log until the first unplanned restart shows them the bill.
A shopping list kept as a notebook where every change is written on a new line - add milk, cross out milk, add bread twice. After a month the notebook is thick and the actual list is six items. Copying those six items onto a fresh page and throwing the old pages away is compaction: the list is unchanged, the history is gone. But while you are copying, the table is occupied, you need both the old pages and the new page in front of you, and anyone who wants to add an item has to wait or be added twice.
saying these in an interview costs you the question
- Thinks the log stops growing once the keyspace stops growing.
- Believes compaction only trims bytes off the front of the file, cheaply.
- Assumes background work is free because no caller is waiting for it.
- Expects compaction to reclaim memory rather than briefly consume more.
- Sees compaction as a disk concern with no bearing on how long a start takes.
- Schedules it at peak traffic on the grounds that it only touches disk.