kafka topic retention vs compaction choice
Explains when to use time/size retention versus log compaction on a Kafka topic. Use it when creating a topic, when disk usage grows unbounded, or when consumers need the latest state per key. Not for consumer offset tuning.
TL;DR
Retention deletes old messages by age or size; compaction keeps only the latest value per key, forever. Use retention for event streams like logs and metrics where history ages out. Use compaction for changelog topics like user profiles or config where the latest value per key is the truth. Never compact a stream you need to replay for history, because old versions are gone.
The query
kafka topic retention vs compaction choiceUse this when
- you are creating a topic and unsure whether to compact it
- disk usage on a topic grows without bound
- consumers need the current state per key, not the full history
Not for
- consumer offset reset or retention of the offsets topic itself
- message ordering guarantees, which are a partition concern
Steps
- Classify the topic: an event stream (append, age out) gets retention; a changelog (key updates, latest wins) gets compaction.
Expected output: You can state which category your topic falls into.
- For retention, set retention.ms and retention.bytes to match how long consumers actually need to replay.
Expected output: Old segments disappear on schedule and disk usage stays bounded.
- For compaction, set cleanup.policy=compact and make sure every message has a meaningful key; keyless messages compact badly.
Expected output: The topic keeps one current record per key.
- Consider compact plus a retention time (cleanup.policy=compact,delete) when you want latest-per-key but also want ancient keys to eventually vanish.
Expected output: Inactive keys expire while active keys stay current.
- Document the choice next to the topic config so the next person does not flip it casually.
Expected output: The topic's purpose and cleanup policy are written down together.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_MgsmM6zo34HJElt8XjLaiA
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.