How to configure kafka topic retention policy during creation with Spring?
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 used extensively for building real-time data pipelines and streaming applications. One of its core components are "topics", where messages are categorized. Each Kafka topic can be configured with various policies, including those governing data retention. Retention policies are crucial for managing how long messages are stored on a topic before being deleted or compacted.
In a Spring application, configuring Kafka topic retention policies can be handled conveniently through the use of Spring Kafka, a project which extends the functionality of Kafka with Spring-friendly features. Below, we discuss how to configure a Kafka topic’s retention policy during its creation, specifically in the context of a Spring application.
Understanding Kafka Topic Retention Policy
Kafka stores messages in topics and the messages in these topics are retained according to the configured retention policy. The two main types of retention policies are:
- Time-Based Retention (
retention.ms): This policy determines how long messages are retained in a topic in terms of time. After this period, messages are eligible for deletion regardless of whether they have been consumed. - Size-Based Retention (
retention.bytes): This policy limits the total size of messages retained on a topic. Once the size limit is reached, the oldest messages are deleted. - Compaction (
cleanup.policy): This is different from deletion as it retains only the last message for each key in the topic.
Configuring Kafka Topics with Spring
Spring provides comprehensive support for Kafka through its Spring Kafka project. Configuring a Kafka topic with a specified retention policy involves defining these settings within the application's configuration class or within an external configuration file (like application.yml or application.properties).
Using @Configuration and @Bean
You can define Kafka topic configurations programmatically in a Spring configuration file using @Bean annotations:
In the above example, a topic named sample-topic is configured using the TopicBuilder API. The retention policy is set to delete messages after 24 hours.
Using application.yml or application.properties
Alternatively, you can configure these properties directly in the application's configuration files:
For application.yml:
And in application.properties:
Key Takeaways
Here's a summary table of the key properties for Kafka topic retention configuration:
| Property | Description | Example Value |
retention.ms | Time after which messages will be deleted. | 86400000 (24 hours) |
retention.bytes | Maximum size of messages before old messages are deleted. | 104857600 (100MB) |
cleanup.policy | Policy for message deletion or compaction. | delete or compact |
Conclusion
Configuring Kafka topic retention policies properly is essential for managing storage and ensuring that your application complies with data policies. With Spring Kafka, this process is streamlined, allowing developers to configure topics directly through code or configuration files, making management and adjustments easier as application requirements evolve.

