How can I set unlimited retention for an compacted topic in Kafka?
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 popular distributed streaming platform commonly used for building real-time streaming data pipelines and applications. Managing data retention and topic compaction in Kafka is a critical aspect for ensuring that data is kept as needed without overwhelming the storage.
Understanding Topic Compaction in Kafka
Topic compaction in Kafka is a mechanism to retain only the last value for each key within a compacted topic. This feature is particularly useful for scenarios where only the most recent state is needed per key, like in databases or caching systems. Compaction ensures that your Kafka topic doesn't grow indefinitely, which could otherwise use up storage resources unnecessarily.
Configuring Unlimited Retention for a Compacted Topic
To configure a Kafka topic to use compaction while also retaining records indefinitely, you need to set appropriate topic configurations during creation or update the configurations of an existing topic. Here are the steps and configurations to consider:
- Create a New Topic with Compaction and Unlimited Retention Use the Kafka command-line tools to set the topic configurations:
In this command:
cleanup.policy=compactconfigures the topic to use compaction.retention.ms=-1sets the retention time to unlimited.
- Update an Existing Topic to Have Unlimited Retention If you have an existing compacted topic and you want to update it to have unlimited retention, use the following command:
Key Points in Configuration
Here is a table that summarizes the key configuration parameters for setting up unlimited retention in a compacted Kafka topic:
| Configuration Parameter | Description | Example Value |
| cleanup.policy | Determines the log cleaner policy | compact |
| retention.ms | Duration to retain a log in milliseconds | -1 (for unlimited) |
Note: Setting retention.ms to -1 disables the time-based retention policy, which Kafka otherwise uses to determine how long to retain log segments.
Deep Dive: How does Compaction Work?
Log compaction in Kafka works by retaining the last known value for each key. The log cleaner thread periodically scans log segments and removes older records if a newer value exists for the same key. This process does not follow the conventional FIFO (first-in, first-out) model of data retention. Instead, it's more about maintaining a state snapshot.
Potential Use Cases
The ability to set unlimited retention for compacted topics in Kafka can be especially useful in scenarios such as:
- Event sourcing systems where each key represents an entity or aggregate root, and its state must be reconstructed by the last known event.
- Configuration data which must be persistently stored and often retrieved without concern for the data aging out.
- Snapshot state applications where only the latest state is relevant, but historical values have no significance.
Conclusion
Setting up unlimited retention for a compacted topic in Kafka allows for more flexibility in data management strategies. It seamlessly combines the benefits of data compaction with the assurance that data will be retained indefinitely. This setup is crucial for Kafka applications that rely on having always-accessible, current value states for keys without experiencing data loss due to retention policies.

