MSK Not Deleting Old Messages
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Amazon MSK and Message Retention
Amazon Managed Streaming for Apache Kafka (MSK) is a fully managed service that makes it easy to build and run applications that use Apache Kafka to process streaming data. Kafka, at its core, is a distributed streaming platform capable of handling trillions of events a day. A fundamental aspect of Kafka is how it handles message retention.
Understanding Kafka Message Retention
Kafka stores messages in topics that are distributed across different partitions and brokers for scalability and resilience. Each message within these partitions has a retention policy that determines how long it is stored before being deleted. The primary configurations that affect message retention are:
retention.bytes: This limits the total size of logs that can be stored in each partition. Once this size is reached, older messages are deleted regardless of their age.retention.ms: This specifies the time duration for which the messages are retained in the topic. After this time, messages are eligible for deletion during the next log cleanup.
When dealing with issues related to MSK not deleting old messages, it often revolves around the mismanagement or misunderstanding of these configurations.
Common Issues and Resolutions
Here are some reasons why old messages may not be deleted in MSK:
- Incorrect Configuration: If the retention policies (
retention.msorretention.bytes) are set too high or effectively disabled (by setting them to-1), messages will not be deleted as expected. - Large Log Segments: Kafka deletes messages in chunks called segments. If the segments are too large, the deletion of old messages might not occur until the entire segment is eligible for deletion.
- Low Traffic: Kafka only checks for old messages to delete during log compactions or when new messages are added. Topics with very low traffic might not trigger these checks frequently.
- Broker Configurations Overridden at Topic Level: Sometimes, the topic-specific configurations might override the broker-level defaults, leading to unexpected retention behaviors.
Technical Example
Suppose an MSK user notices that messages are not being deleted from a topic despite having a retention.ms set to 604800000 (one week). This scenario might involve checking configurations at both the topic and broker level:
If the settings are correct at the topic level, review the broker settings and ensure they are not inadvertently influencing topic behavior.
Best Practices for Managing Retention
To efficiently manage message retention in MSK, consider the following practices:
- Set Appropriate Retention Policies: Based on your application's data needs and storage capacity, configure suitable retention policies.
- Monitor Disk Usage Regularly: Always monitor the disk usage of your MSK clusters to anticipate when to adjust retention policies or scale storage.
- Use Topic-Level Overrides Judiciously: Be cautious when setting topic-level configurations that override broker defaults.
Summary Table
| Configuration | Description | Typical Value | Implications |
retention.bytes | Maximum log size per partition | 1GB (1073741824) | Deletes old messages if log size exceeded |
retention.ms | Maximum message retention time | 7 days (604800000) | Deletes messages older than the specified duration |
| Segment Size | Size of log file chunk | 1GB | Larger segments may delay deletions |
| Traffic Level | Frequency of Message Ingestion in Topics | Varies | Lower traffic can delay cleanup processes |
Conclusion
Understanding and configuring message retention in Amazon MSK requires a careful balance between performance, cost, and compliance requirements. While Kafka provides robust mechanisms to manage data lifecycle, properly setting up and monitoring these configurations is key to ensuring data is handled as expected.

