Make Kafka Topic Log Retention Permanent
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a powerful distributed streaming platform that can handle high volumes of data and allows numerous producers and consumers to read and write data simultaneously. Among its core features is the capacity for managing log data retention, critical for effective data management and compliance with data storage requirements. This article explains how to make Kafka topic log retention permanent, ensuring that data stored in Kafka topics is retained indefinitely unless explicitly deleted by policies or operations.
Understanding Kafka Topic Log Retention
In Kafka, every topic consists of partitions, where partitions are essentially append-only logs. Each record within these partitions is assigned a sequential ID called offset. Kafka stores these logs for a configurable duration before they are deleted, which helps in managing the storage and ensuring the system does not run out of space.
The retention policy in Kafka is controlled by a set of parameters that dictate how long the data should be retained in a topic. These settings can be specified at the broker level or overridden at the individual topic level.
Key Configuration Parameters:
- log.retention.hours: This is the default retention policy that determines how long Kafka logs are retained in hours. It defaults to 168 hours (or 7 days).
- log.retention.bytes: Defines the maximum size in bytes before old data is discarded from a topic.
- log.retention.minutes and log.retention.ms: Alternative configurations for time-based retention in minutes and milliseconds, respectively.
Setting Up Permanent Retention
To set up permanent retention for a Kafka topic, it’s critical to adjust these parameters correctly. Setting up permanent retention effectively means data should never be automatically deleted based on time or size. Here’s how you can configure it:
Step 1: Disable Time-Based Retention
Set the time-based retention setting to a value that indicates data should never be deleted. You can use the special value -1:
These settings can be applied globally to all topics on a broker or they can be set per topic using topic-specific configuration.
Step 2: Disable Size-Based Retention
Similarly, disable size-based retention by setting:
This setting ensures that logs are not deleted based on their size. Like time-based settings, this can also be specified globally or per topic.
Step 3: Apply Configuration Changes
Changes can be applied without downtime using Kafka's kafka-configs.sh utility. To modify topic-level configurations, you can use:
Replace <zookeeper-host>:<port> with your ZooKeeper configuration, and <topic-name> with the name of your Kafka topic.
Considerations and Best Practices
While configuring permanent retention, consider the implications:
- Storage Management: Ensure your system has adequate storage to handle ever-growing data as Kafka will no longer automatically delete logs.
- Performance: More data in topics can lead to longer recovery times and could impact performance. Regular monitoring and optimization of performance are crucial.
- Compliance and Legal: Always consider legal and compliance aspects related to data retention.
Retention vs. Compaction
Another concept in Kafka retention is log compaction, which keeps only the latest value for each key in a topic. This might be useful if you need to maintain a permanent record but only care about the most recent state.
Summary
| Parameter | Setting | Description |
| log.retention.hours | -1 | Disables time-based retention (never delete based on time). |
| log.retention.bytes | -1 | Disables size-based retention (never delete based on size). |
| log.retention.minutes | -1 | Alternative time-based configuration in minutes. |
| log.retention.ms | -1 | Alternative time-based configuration in milliseconds. |
Permanent retention in Kafka is crucial for scenarios where data loss cannot be tolerated. By carefully tweaking Kafka's retention settings, organizations can ensure that their data persists indefinitely, however, it's essential to manage the resultant data growth effectively to maintain system health and performance.
Related reading
- Make RabbitMQ durable/persistent queues survive Kubernetes pod restart
- Managing Kafka Topic with spring
- Masstransit use RabbitMQ is very slow performance?
- MassTransit with RabbitMQ recovering the error queue
- MassTransit with RabbitMQ When is a message moved to the error queue
- master node in multi-node kafka cluster
- Max number of messages that can be stored in a Kafka topic partition?
- Max number of tuple replays on Storm Kafka Spout

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.