Skip to content
BENIBIX Learn

Kafka Retention

Also known as Kafka Retention Period, Topic Retention, retention.ms

How long a Kafka topic keeps its events before it deletes them. The default is 7 days.

Think of it like

It is like a phone that wipes old messages after a week. It does not wait for you to hear them.

Example

A team keeps the clicks topic for 30 days. A new report can then read a full month of clicks from the start.

Why it matters

It decides how far back any reader can go. If it is too short, a broken reader loses data before it is fixed.

Definition

Kafka retention is how long a topic keeps its events. The default is 7 days, and each topic can set its own. Old events are deleted when the time runs out, whether anyone read them or not. You can set a size limit instead, or keep events forever. A second mode, called compaction, keeps only the latest event for each key. Retention is what lets a new reader replay the past.

Example code

console
$ bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter \
    --entity-type topics --entity-name clicks \
    --add-config retention.ms=2592000000
Keep the clicks topic for 30 days. The time is written in milliseconds.
The top row is retention. Day 1 drops off once it is older than 7 days, read or not.

Common mistake

Treating Kafka as the only copy of the data. After the retention time, the events are gone for good.

Key takeaways

  1. 1
    Time or size. Delete by age or by bytes.
  2. 2
    Read or not. Old events go either way.
  3. 3
    Or compact. Keep only the latest per key.