Retention & Compaction
Deleting old data by time and size, and keeping the latest value per key
Kafka keeps data until a cleanup policy removes it. There are two,
and they answer different questions: delete asks "how long do I keep
history?", compact asks "what's the latest value for each key?"
cleanup.policy=delete (the default)
Whole closed segments are deleted when they're older than
retention.ms or when the partition is bigger than retention.bytes:
Retention deletes segments, not records. A record becomes deletable
only when its whole segment is past retention, and the active segment
is never deleted. On a quiet topic with 1 GB segments and 7-day
retention, a segment can hold weeks of data before it rolls, so data can
live much longer than retention.ms. Lower segment.ms if retention
must be enforced precisely (for example, for privacy rules).
retention.bytes applies per partition: a 12-partition topic with
1 GiB retention.bytes can hold 12 GiB per replica, times the
replication factor across the cluster. Size disks from that number.
cleanup.policy=compact
Compaction keeps at least the latest record for every key and removes older records with the same key. The topic becomes a changelog you can replay to rebuild current state: the latest profile per user, the current config per service.
Run in this module's lab, with settings that make the cleaner run quickly:
kafka-topics.sh $B --create --topic user-profiles --partitions 1 \
--config cleanup.policy=compact \
--config segment.ms=2000 \ # roll segments fast so they can be cleaned
--config min.cleanable.dirty.ratio=0.01 \
--config delete.retention.ms=1000
# three rounds: alice/bob/carol -> email-v1, then v2, then v3
# then a TOMBSTONE for carol (key with a null value), then dave -> email-v1
# ...wait ~40 seconds for the log cleaner...
kafka-console-consumer.sh $B --topic user-profiles --from-beginning \
--formatter-property print.key=true --formatter-property print.offset=true
Offset:6 alice email-v3
Offset:7 bob email-v3
Offset:9 carol null # the tombstone itself, kept for delete.retention.ms
Offset:10 dave email-v1Offsets 0-5 and 8 are gone, and the survivors keep their original offsets. Compaction never renumbers; consumers just see gaps.
Tombstones
A record with a key and a null value is a tombstone: "this key is
deleted." Compaction removes older records for that key, keeps the
tombstone itself for delete.retention.ms (24 hours by default) so that
slow consumers get to see the delete, and then removes it too.
How the cleaner works
Each partition has a clean part (already compacted) and a dirty
part (written since the last pass). A background log cleaner thread
picks the partition with the highest dirty ratio above
min.cleanable.dirty.ratio (0.5 by default), builds an in-memory map of
key → latest offset from the dirty part, and rewrites the old segments
keeping only records that are the latest for their key.
cleanup.policy=compact,delete does both: compact, and also delete
segments past retention. That's useful for a changelog where very old keys
should eventually disappear.
Kafka's own internal topics are compacted: __consumer_offsets (latest
committed offset per group and partition), Connect's config and offset
topics, and Schema Registry's _schemas. If compaction stops (check
/opt/kafka/logs/log-cleaner.log on each broker, and the cleaner's metrics), those topics grow
without bound and broker restarts slow to a crawl.
Choosing
| Data | Policy |
|---|---|
| Events: clicks, orders placed, payments | delete, retention = how far back consumers may need to replay |
| Current state per entity: profiles, inventory, config | compact |
| CDC streams from a database | compact (rebuild a table), often with a long delete too |
| Audit logs that must be kept | delete with long retention, or tiered storage |
Tiered storage
Kafka 3.6+ can offload closed segments to object storage (S3, or an S3-compatible store such as SeaweedFS) while
keeping recent ones on local disk, via remote.storage.enable=true and a
remote storage plugin. Brokers then hold days of data locally and months in
cheap storage, with consumers reading old data transparently. It's a
cluster-level feature that needs a plugin, so it's not part of this lab.
Lab
- Reproduce the compaction run above. Before the cleaner runs, consume from the beginning and count records; after, count again.
- Set
retention.ms=60000andsegment.ms=10000ondemo, produce, wait two minutes, and checkkafka-get-offsets.sh --time -2(earliest offset) moving forward. - Describe
__consumer_offsetsand confirm itscleanup.policy.