Apache Kafka
Retention Period
Data Management
Kafka Configuration
Stream Processing

Kafka - Retention period Parameter

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform that primarily functions to publish, subscribe to, store, and process streams of records in real time. A critical aspect of Kafka's design revolves around its handling of data retention, which dictates how long data is kept before it is deleted or compacted. Understanding the retention period parameter in Kafka is crucial for managing storage and ensuring data availability for consumers over time.

Understanding Retention Policies

Kafka offers two primary ways to control data retention:

  1. Time-based retention: Data is retained for a specific duration (e.g., seven days). After that period, old data is eligible for deletion.
  2. Size-based retention: Data is retained until the total size of the stored logs reaches a specified threshold.

These retention policies can be set at the broker level (affecting all topics) or overridden at the individual topic level.

Key Parameters for Retention

The primary configurations related to retention in Kafka are:

  • log.retention.hours, log.retention.minutes, and log.retention.ms: These parameters configure the retention period in hours, minutes, or milliseconds, respectively. If all three are set, the smallest specified unit takes precedence.
  • log.retention.bytes: This parameter sets the maximum size in bytes of the log files that are retained on the Kafka broker. If the size of the log exceeds this setting, old log segments are deleted to stay within the limit.
  • log.segment.bytes: This setting controls the size of the log segments. When a log segment reaches this size, it is closed, and a new segment is started.
  • log.segment.ms: This parameter determines the time Kafka will wait before closing an active segment file.

Example Scenarios

Consider a Kafka topic with the following settings:

  • log.retention.hours=168 (one week)
  • log.retention.bytes=1073741824 (1GB)

Here, messages in the topic are deleted if they are older than 168 hours or if storing all messages exceeds 1GB, whichever happens first.

How Retention Impacts Kafka Performance

Retention settings can significantly impact Kafka's performance:

  • Disk Space Usage: Proper retention policies ensure that disk space does not grow indefinitely, which is crucial for maintaining Kafka's performance and avoiding system crashes due to disk fill-up.
  • Consumer Data Availability: If data is retained for an extended period, consumers have more time to process messages. However, longer retention can lead to increased disk usage.
  • Broker Performance: More stored data can lead to longer recovery times for the broker and higher latencies during data fetch operations.

Table: Summary of Retention Parameters and Their Impacts

ParameterDescriptionTypical ValueImpact on System
log.retention.hoursDuration after which log segments are eligible for deletion.168 (7 days)Controls disk space by removing old data.
log.retention.bytesMaximum size of log segments before older segments are deleted.1GBPrevents excessive disk usage.
log.segment.bytesSize threshold for log segment files.1GBBalances between fewer large files and many small files.
log.segment.msTime after which to close the current log segment.1 weekImpacts file segment management and broker performance.

Best Practices for Managing Retention

  • Balance between retention needs and available resources: Set retention policies that align with your storage capabilities and the consumers' need to access historical data.
  • Monitor and adjust: Regularly monitor disk usage and consumer performance. Adjust retention settings as necessary to optimize performance.
  • Use topic-level overrides carefully: While it's possible to set different retention settings per topic, managing many custom configurations can become complex.
  • Consider using compacted topics for key-based retention: For use cases that benefit from retaining the latest value per key, consider using Kafka's log compaction feature instead of relying solely on time or size-based retention.

Understanding and configuring the retention period parameter in Kafka is essential for managing how long data should remain available and ensuring efficient resource usage, which are key to maintaining high system performance and reliability. Adjusting these settings according to specific use cases and system capabilities will help optimize Kafka's functionality for your needs.


Course illustration
Course illustration

All Rights Reserved.