How to set cleanup.policy 'delete' AND 'compact' for a Kafka topic?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a distributed event streaming platform, enables developers to build robust messaging and streaming applications. One of its core features is the ability to configure how messages are retained and managed within a topic through the cleanup.policy setting. This policy can significantly influence disk space utilization and access to historical data.
Understanding cleanup.policy
Before discussing how to set the cleanup.policy to both delete and compact for a Kafka topic, it's important to understand what these terms mean:
- Delete Policy: This policy instructs Kafka to delete messages either after a specified retention time or after the log reaches a certain size.
- Compact Policy: Compaction ensures that a Kafka topic retains only the last message for each key. This is crucial for topics that act as a state store or changelog.
Setting cleanup.policy
To configure a Kafka topic to use both deletion and compaction, follow these steps:
- Create a new topic with both policies: You can use the Kafka command-line tools to set the
cleanup.policyto bothdeleteandcompact. This ensures that older messages are removed based on time or size, but the last message for each key is always retained.
--config cleanup.policy=compact,deletesets the topic to utilize both compaction and deletion policies.retention.ms=1680000configures the topic to retain messages for a specified amount of time (in milliseconds).segment.bytes=1073741824sets the maximum size of a log segment file. After this size is reached, old segments are checked for eligibility for expiration or compaction.
- Modify cleanup policy for an existing topic: If you need to modify an existing topic to use both policies, you can alter the topic configuration:
- Verify Topic Configuration: It's good practice to verify that the configurations have been set as expected:
This command will show all the configurations for your topic including the set cleanup.policy.
Recommended Configurations and Considerations
When setting up your topic with both deletion and compaction, consider the following recommendations:
- Message Retention: Set a reasonable
retention.msto ensure that messages are not deleted too quickly, which might lead to data loss. - Segment Tuning:
segment.bytesshould be tuned based on the amount of data typically written to the topic and disk space considerations. - Performance Impact: Keep in mind that compaction is a resource-intensive process. Monitor performance and adjust configurations as necessary.
Summary Table of Key Configurations
| Configuration | Description | Example Value |
cleanup.policy | Type of cleanup used, delete and/or compact. | compact,delete |
retention.ms | Time a message is retained before deletion. | 1680000 (about 28 minutes) |
segment.bytes | Maximum segment file size. | 1073741824 (1GB) |
Additional Details
- Using the Admin Client API: You can also configure these settings programmatically using Kafka’s Admin Client API.
- Impact of Misconfiguration: Incorrect settings can lead to unexpected disk usage or loss of messages. Always ensure your settings are validated in a test environment before applying in production.
Conclusion
Effectively combining the delete and compact policies in Kafka topics ensures that your application can manage storage efficiently while retaining necessary historical data. This setup is critical for applications requiring a balance between immediate data availability and long-term storage efficiency. Be sure to test different configurations to find the optimal setup for your specific requirements.
Related reading
- How to set consumer.id for new Kafka consumer
- How to set group name when consuming messages in kafka using command line?
- How to set group.id for consumer group in kafka data source in Structured Streaming?
- How to set kafka-connect connector and task's JVM heap size?
- how to set kafka consumer concurrency using spring boot
- How to set Kafka parameters from a properties file?
- How to set offset committed by the consumer group using Spark's Direct Stream for Kafka?
- How to set offset in kafkalistener

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.