Learn Labs
Storage

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.ms=6048000007 days (cluster default here: log.retention.hours=168)retention.bytes=10737418241 GiB per PARTITION (not per topic); -1 = unlimitedsegment.bytes=268435456256 MiB segmentssegment.ms=604800000also roll a segment after 7 days, even if not full

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-v1

Offsets 0-5 and 8 are gone, and the survivors keep their original offsets. Compaction never renumbers; consumers just see gaps.

beforealicev1bobv1carolv1alicev2bobv2carolv2alicev3bobv3carolv3carolnulldavev1offsets 0-10afteralicev3bobv3carolnulldavev1offsets 6, 7, 9, 10 survivetombstone: kept for delete.retention.ms

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.

min.cleanable.dirty.ratio=0.5clean when half the log is uncompactedmin.compaction.lag.ms=0minimum age before a record may be compacted awaymax.compaction.lag.ms=...force compaction of anything older than thisdelete.retention.ms=86400000how long tombstones survive

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.

Choosing

DataPolicy
Events: clicks, orders placed, paymentsdelete, retention = how far back consumers may need to replay
Current state per entity: profiles, inventory, configcompact
CDC streams from a databasecompact (rebuild a table), often with a long delete too
Audit logs that must be keptdelete 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

  1. Reproduce the compaction run above. Before the cleaner runs, consume from the beginning and count records; after, count again.
  2. Set retention.ms=60000 and segment.ms=10000 on demo, produce, wait two minutes, and check kafka-get-offsets.sh --time -2 (earliest offset) moving forward.
  3. Describe __consumer_offsets and confirm its cleanup.policy.

On this page