Compacted topics are how Kafka holds state: the latest customer record per ID, the current configuration per tenant, the changelog behind a Kafka Streams store. They are also a frequent source of surprise, because compaction is a background cleanup process and not a query guarantee. This note covers the mechanism, the settings that matter, and the mistakes we see most often.
What compaction guarantees
With cleanup.policy=compact, Kafka retains at least the last record for every key. That is the whole promise. It does not promise that older records are gone, that they are gone by any particular time, or that a consumer will never read two records for the same key.
A consumer reading from the beginning of a compacted topic will see the latest value for each key, plus whatever not-yet-cleaned older values are still in the log. Consumers must treat the topic as a stream of updates where the last one wins.
How the cleaner works
Each partition log is split into segments. The active segment is never compacted. The cleaner only works on closed segments, and it picks logs whose "dirty" portion (records not yet cleaned) is large enough.
The settings that decide when that happens:
min.cleanable.dirty.ratio(default 0.5): the fraction of the log that must be dirty before the cleaner considers it. Lower values clean sooner at the cost of more broker I/O.segment.ms/segment.bytes: control when the active segment rolls and becomes eligible. On a low-volume topic with the default 7-daysegment.ms, a record can sit uncompacted for a week or more.min.compaction.lag.ms: a floor on how long a record stays uncompacted. Useful when consumers need to see every intermediate update for some period.max.compaction.lag.ms: a ceiling that forces eligibility, which matters for deletion deadlines such as privacy requests.log.cleaner.threadsandlog.cleaner.dedupe.buffer.size(broker): cleaner capacity. A cleaner thread that dies on a bad segment stops compaction silently for every partition it owned, so alert on cleaner health, not just on topic size.
Tombstones and deletion
A record with a key and a null value is a tombstone. Compaction eventually removes earlier values for the key, and the tombstone itself is kept for delete.retention.ms (default 24 hours) before it is removed too.
The operational consequence: a consumer that is offline longer than delete.retention.ms can miss the tombstone entirely. If it is rebuilding a local copy from a mid-stream position, it may keep a key that was deleted. Either keep delete.retention.ms longer than your worst-case consumer downtime, or force such consumers to reseed from the beginning of the topic.
Requirements that trip people up
- Every record needs a key. Producing a null-key record to a compacted topic is rejected by the broker. Keys also determine partitioning, so the same key must always reach the same partition, or compaction cannot dedupe across the split.
- Changing the partition count breaks key placement. Adding partitions remaps keys with the default partitioner. Old and new values for the same key may then live in different partitions, and ordering for that key is lost. Size partitions up front (see our partition sizing note).
- Compaction does not reorder. Offsets of retained records stay the same; gaps appear. Code that assumes contiguous offsets will misbehave.
compact,deleteis a valid combination. It compacts and also drops records older thanretention.ms. Use it when you want latest-value semantics with a bounded history, and be explicit that old keys will eventually vanish entirely.- Transactional markers and compaction. Control records are cleaned up by the same process, so a topic written transactionally can look sparser than the data volume suggests. This is expected.
A sizing and monitoring checklist
- Estimate disk as (distinct keys × average value size × replication factor), plus headroom for the dirty portion, which can be up to roughly the ratio you configured.
- Track the cleaner's max-clean-time and
max-dirty-percentmetrics, and the number of log segments per partition. Growth in dirty percent with flat traffic means the cleaner is not keeping up. - Test a deletion path end to end: produce a value, produce a tombstone, wait out your configured lags, and confirm a fresh consumer no longer sees the key.
- For topics feeding Kafka Streams or Connect, check the changelog and offsets topics for the expected policy after any provisioning change. A state-store changelog accidentally created with
deleteinstead ofcompactwill restore incompletely once retention expires.
When compaction is the wrong tool
If you need an audit trail of every change, use a normal retention topic and derive the compacted view separately. If you need a point-in-time query, a database is the better home. Compaction suits the narrow case it was built for: rebuilding current state from a log, cheaply and repeatedly.
If you are unsure how your compacted topics are behaving, a Kafka Health Check reviews cleaner configuration, tombstone settings, and consumer reseed paths, with findings ranked by production risk.