Scrivio Interview library
InternalsIntermediate7 min read · updated 13 November 2026

Log compaction is the alternative to time-based deletion: instead of dropping old segments, Kafka keeps the latest record for each key, and the work happens in the background on every broker.

Explain how the log cleaner compacts a topic with cleanup.policy=compact: how it chooses what to clean, how cleaning works, and what a consumer reading from offset 0 is guaranteed to see. Name the settings that control the timing.

Cleaner threads pick the partition log with the highest dirty ratio, the share of bytes written since its last clean, among logs above min.cleanable.dirty.ratio (0.5 by default) or past max.compaction.lag.ms. The cleaner scans the dirty section to build an offset map from each key to its latest offset, then rewrites the older segments keeping only records that are still the latest for their key, merging small segments as it goes; offsets and order are kept, with gaps, and the active segment is never touched. A consumer reading from offset 0 is guaranteed at least the latest value of every key that was not deleted, and may also see older values not yet cleaned; tombstones are kept for delete.retention.ms so consumers see deletes.

Why this question is asked

Compacted topics are how Kafka stores state: the latest address of every customer, the current config of every device, the committed offsets of every consumer group in __consumer_offsets. Their guarantees are different from normal topics and easy to misunderstand, which leads to bugs such as services that miss deletes or depend on seeing every intermediate value. Interviewers ask about the cleaner to see whether we know what compaction promises and when it happens. In this answer, we will recap what compaction keeps, see how the cleaner chooses a log, follow one cleaning pass, look at the timing settings and tombstones, and state exactly what a consumer from offset 0 sees.

1A Quick Recap of Compaction

With cleanup.policy=compact, Kafka does not delete data by age. Instead it removes records that have been superseded: when a newer record with the same key exists later in the log, the older one is eventually removed. Over time the topic converges to roughly one record per key, its latest value, like a table.

The work is done by the log cleaner, a set of background threads on every broker (log.cleaner.threads, 1 by default) that compact the partitions that broker hosts.

2How the Cleaner Chooses What to Clean

The cleaner divides each compacted log into two parts. The clean section is everything up to the point where the last cleaning pass stopped, so each key appears at most once there. The dirty section is everything written since, which may contain new values for keys in the clean section. The broker remembers the boundary per partition in a cleaner-offset-checkpoint file.

The dirty ratio of a log is dirty bytes divided by the total bytes of the clean and dirty sections. When a cleaner thread is free, it picks the log with the highest dirty ratio among those whose ratio exceeds min.cleanable.dirty.ratio, 0.5 by default, meaning at least half the log is new since the last clean. A log is also eligible regardless of the ratio if it has records older than max.compaction.lag.ms that have not been cleaned.

3One Cleaning Pass, Step by Step

First the cleaner scans the dirty section and builds an offset map: for every key it sees, the latest offset at which that key appears. The map stores a hash of the key and the offset, and its total size across threads is log.cleaner.dedupe.buffer.size (128 MB by default). If the dirty section has more keys than the map can hold, the pass covers only as much of the dirty section as fits, and the rest waits for the next pass.

Then the cleaner recopies the log from the beginning up to the end of the part it mapped. For each record, it looks up the key: if the map says a later offset exists for that key, the record is dropped; otherwise it is kept. Kept records are written into new segment files, and several small old segments are merged into one where they fit within segment size limits. When a group of segments is rewritten, the new files are swapped in for the old ones, and the clean boundary moves forward.

Java
// dirty section: k1@105=v3, k2@106=v2, k3@107=null (tombstone)
// offset map built from it:
// k1 -> 105, k2 -> 106, k3 -> 107
// recopy from offset 0
offset 100 k1=v1 // dropped: k1 is later at 105
offset 101 k2=v1 // dropped: k2 is later at 106
offset 102 k4=v1 // kept: k4 not in the map
offset 103 k3=v1 // dropped: k3 is later at 107
offset 105 k1=v3 // kept
offset 106 k2=v2 // kept
offset 107 k3=null // kept for now, as a tombstone
// result offsets: 102, 105, 106, 107 (gaps, same order)
Step throughOne pass of the log cleaner
  1. Pick the dirtiest eligible logratio above min.cleanable.dirty.ratio
  2. Scan the dirty sectionbuild key -> latest offset map
  3. Recopy segments from the startskip the active segment
  4. A later offset for this key?
    YesDrop the recordNoKeep it with its offset
  5. Swap cleaned segments inmerge small segments
  6. Move the clean boundary forward

6 steps in order. Play them, or step through them one at a time.

The map is built from new data, then applied to the whole older log.

Notice that offsets never change. Kept records keep their original offsets, so the cleaned log has gaps, and consumers handle gaps naturally because they only ever ask for the next offset after the last one they read. Order is preserved too, which keeps the per-key ordering guarantee intact.

4Timing Settings and the Active Segment

The active segment is never compacted, because it is still being written. So records in it stay as they are until it rolls, which on a quiet partition can take up to segment.ms. min.compaction.lag.ms protects recent records: a record younger than this is not compacted even if superseded, which is useful when consumers need a window to see every intermediate value. max.compaction.lag.ms works the other way, forcing a log to become eligible for cleaning once uncleaned data is older than the bound, even if the dirty ratio is low; together with segment.ms it caps how long a superseded value can survive.

5Tombstones and What a Consumer From Offset 0 Sees

Deleting a key in a compacted topic is done by writing a record with that key and a null value, called a tombstone. The cleaner treats it like any other latest value, so it removes older values of the key, but it keeps the tombstone itself for delete.retention.ms (one day by default) after the cleaning pass that first reached it. That gives consumers time to see the delete. After that period, a later pass removes the tombstone, and the key disappears from the log entirely.

So the guarantee for a consumer that reads from offset 0 and reaches the end is: it sees at least the latest value of every key that has not been deleted, in offset order. It may also see older values that have not been cleaned yet, for example in the active segment or in a dirty section, so consumers must treat the topic as a sequence of updates and apply each one, not assume one record per key. And it sees a tombstone for a deleted key only if it reaches that tombstone before delete.retention.ms has passed since it was cleaned.

6Follow-up questions

Why must records in a compacted topic have a non-null key?

Compaction works by key: the cleaner keeps the latest record per key, and without a key there is nothing to compare, so there is no way to know whether a record was superseded. Brokers therefore reject records without a key on a topic with cleanup.policy=compact, and the producer gets an invalid record error. A null value is fine, because that is a tombstone; a null key is not.

Can a topic be both compacted and time-limited?

Yes, with cleanup.policy=compact,delete. Compaction keeps the latest value per key, and retention also deletes segments older than retention.ms, so even latest values eventually expire. It suits caches of recent state where old keys should disappear.

7Conclusion

In this answer, we saw that the log cleaner picks the partition with the highest dirty ratio above min.cleanable.dirty.ratio, builds a key-to-latest-offset map from the dirty section within log.cleaner.dedupe.buffer.size, and recopies older segments keeping only the latest record per key, merging small segments, with offsets and order preserved and the active segment untouched. min.compaction.lag.ms, max.compaction.lag.ms and segment.ms control the timing, tombstones are kept for delete.retention.ms, and a consumer from offset 0 sees at least the latest value of every live key, possibly with some older values too.

What to say in the interviewAbout 90 seconds aloud

Open with

Compaction keeps the latest record per key instead of deleting by age, and the log cleaner threads on each broker do it in the background. Each compacted log has a clean section, already compacted, and a dirty section written since.

Then, if they want more

A cleaner thread picks the log with the highest dirty ratio, dirty bytes over total, among those above min.cleanable.dirty.ratio, which is 0.5 by default, or with uncleaned data older than max.compaction.lag.ms. It scans the dirty section and builds an offset map from each key's hash to the latest offset for that key, sized by log.cleaner.dedupe.buffer.size.

Then it recopies the older segments from the start, dropping any record whose key appears later in the map, keeping the others with their original offsets, and merging small segments, and finally swaps the cleaned segments in. Offsets and order are kept, with gaps.

The active segment is never compacted, min.compaction.lag.ms protects recent records, and max.compaction.lag.ms forces cleaning within a bound. Deletes are tombstones, records with a null value, which are kept for delete.retention.ms, one day by default, after cleaning so consumers can see them.

So a consumer reading from offset 0 is guaranteed to see at least the latest value of every key that was not deleted, and it may also see older values that are not cleaned yet, so it should apply every record as an update.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how the log cleaner compacts a topic with cleanup.policy=compact: how it chooses...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/d5d2eeb44f/