Kafka-streams setting internal topics cleanup policy to delete doesn't work
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka Streams is a client library for building applications and microservices, where the input and output data are stored in Kafka clusters. It allows for stateful and stateless transformations, aggregations, and joins on streaming data. Kafka Streams manages much of the complexity of dealing with distributed, scalable, and fault-tolerant applications internally. One crucial aspect of managing state in Kafka Streams is through the use of internal topics, primarily for state stores and repartitioning of data.
Cleanup Policy: delete vs. compact
Kafka topics can be configured with different cleanup policies to manage how old data is discarded. There are two main policies:
delete: This policy will delete records once they reach a certain age or size. This is a common policy for topics that do not require a history of all data.compact: This policy is used in scenarios where the complete history of records for a specific key is not needed, but the latest value for each key is crucial. Compact retains at least the last known value for each key.
Issue with Setting Cleanup Policy to delete for Kafka Streams Internal Topics
Setting the cleanup policy to delete for Kafka Streams internal topics generally does not produce the intended behavior, especially for stateful applications. Kafka Streams uses these topics to store the state (KTables) or to shuffle data when re-partitioning is needed. Here's why using delete can be problematic:
- Loss of State on Failure or Rebalance: Internal topics often store state that is critical for the correct functioning of the application. With the
deletepolicy, if a failure or rebalance occurs, you might lose essential data that wasn't backed up elsewhere. - Incorrect Application Results: For stateful operations that rely on windowing or joining over a period, using
deletecan lead to missing records that are needed to compute results accurately.
Example of a Potential Fail Scenario with delete
Consider a Kafka Streams application that maintains a running total of sales per store in a KTable. The internal topic configured with the delete policy might remove older record data needed for the proper functioning of the app under certain operations like app restarts or rebalances.
In this simplistic example, should any message in the salesByStore internal topic be deleted prematurely because of the delete policy, the state recovery or application restart might show incorrect totals, as not all original sales records are available to reconstruct the KTable.
Best Practices and Solutions
To mitigate the issues associated with the wrong cleanup policy in Kafka Streams:
- Default to
compact: For most Kafka Streams use cases involving state, default to using thecompactcleanup policy. This ensures that the latest state is always retained. - Explicitly Set Policies on Internal Topics: When creating streams, explicitly set the cleanup policy on internal topics, if the default does not suit your needs.
Example Code
Summary Table
| Feature | Cleanup Policy delete | Cleanup Policy compact |
| Data Retention | Deletes based on age/size | Retains at least the last value per key |
| Suitable for | Non-critical transient data | Stateful application requirements |
| Risk | High, due to potential data loss | Lower, more controlled data management |
Conclusion
Setting internal topics cleanup policy to delete in Kafka Streams applications, particularly those with stateful operations, should be approached with caution. Misconfiguration can lead to incorrect data processing and potential loss of crucial state information. Adopting the compact policy by default and understanding the implications of different cleanup policies are key steps in optimizing Kafka Streams applications for reliability and correctness.
Related reading
- Kafka-topics --list using ssl
- kafka-topics.sh ---delete --topic ''testTopic'' is not working for kafka V 0.10.1
- Kafka-topics.sh authentication
- Kafka10.1 heartbeat.interval.ms, session.timeout.ms and max.poll.interval.ms
- Kafka - Broker fails because all log dirs have failed
- Kafka - Broker Group coordinator not available
- Kafka - Best practices in case of slow processing consumer. How to achieve more parallelism?
- Kafka - Broker Message size too large

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.