Kafka doesn't delete old messages in topics
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 event streaming platform known for its high throughput and durability. It is designed to handle data streams from multiple sources and deliver them to various consumers. One of Kafka's key features is the management of message retention within its topics. However, users sometimes observe that Kafka does not delete old messages as they might expect. Understanding why this occurs requires a deeper look into Kafka's architecture and configuration settings.
Topic and Partition Basics
In Kafka, topics are categories or feed names where messages are stored. Each topic is split into one or more partitions, allowing topics to scale by distributing data across multiple nodes in a Kafka cluster. Data in each partition is ordered and immutable, with each message in a partition assigned a unique, sequential identifier called an offset.
Retention Policies
Kafka offers several mechanisms to manage the retention of messages in a topic:
- Time-Based Retention: The default retention policy in Kafka is time-based, where messages are retained for a specific period (e.g., 7 days). After this period, messages are eligible for deletion.
- Size-Based Retention: Kafka can also be configured to retain messages based on the total size occupied by the messages in a log. When the size limit is reached, the oldest messages are deleted to make space for new messages.
- Compaction: This retention policy is useful for situations where only the last state of a key is relevant. Here, Kafka ensures that the log retains at least the last known value for each key.
Reasons for Non-Deletion of Messages
Despite the presence of these retention policies, there are several reasons why Kafka might not delete old messages:
- Time or Size Retention Not Reached: If the configured retention period or size has not yet been reached, messages will remain in the log.
- Consumer Offsets: If a consumer group has not updated its offset, Kafka retains the messages to allow the consumer to resume from its last offset.
- Log Compaction: During log compaction, Kafka does not delete messages with unique keys, even if they seem old, as they represent the latest state.
- Misconfiguration: Incorrect configuration of retention settings can lead to messages being retained longer than expected.
- Broker Configuration Issues: Sometimes, settings at the broker level could override topic-specific configurations, leading to unexpected retention behaviors.
Impact of Not Deleting Messages
When old messages are not deleted, it can lead to increased storage costs and potential performance degradation, particularly as the size of the data grows. It is crucial for administrators and developers to carefully manage retention policies to balance between data availability and resource utilization.
Example: Time-Based Retention Configuration
A common configuration for time-based retention in Kafka is to set the retention period using the log.retention.hours parameter in the broker configuration or overwrite it at the topic level as shown below:
Summary Table
Here is a summary of key points regarding Kafka's message retention and non-deletion issue:
| Factor | Description | Potential Impact |
| Retention Not Reached | Time or size limits not reached; messages persist in the log. | Unused data occupies storage. |
| Active Consumer Offsets | Consumers have not progressed; offsets keep messages reachable. | Data accessibility vs. storage cost. |
| Log Compaction | Compaction retains at least the latest message per key. | Ensures message state is up-to-date. |
| Misconfiguration | Incorrect settings at topic or broker level. | Unintended message retention. |
| Broker Configuration | Global settings may override local topic settings. | Conflicts in retention behavior. |
Conclusion
Proper management and understanding of Kafka's message retention policies are essential for optimizing the performance and storage of a Kafka cluster. Administrators need to adjust configurations carefully, monitor topic and broker behaviors, and ensure that the system handles data retention as expected. Regular audits and updates to the configuration can aid in ensuring that Kafka efficiently manages data, delivering high availability and performance while controlling costs.

