How to configure kafka topic retention policy during creation with Spring?
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 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.
Related reading
- How to configure logging for Kafka producers?
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ using Active/Passive High Availability architecture
- How to configure logback in spring-boot for ANSI color feature?
- How to configure oAuth2 with password flow with Swagger ui in spring boot rest application
- How to configure the time it takes for a kafka cluster to re-elect partition leaders after stopping and restarting a broker?
- How to connect Apache Kafka with Amazon S3?

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.