Kafka Streams Is it possible to have compact,delete policy on state stores?
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 used for building applications and microservices that process and analyze data stored in Kafka. It forms a core part of the stream processing capabilites of Apache Kafka, enabling real-time data processing.
Understanding Kafka Streams State Stores
State stores in Kafka Streams are local storages associated with stream processors. They allow for stateful operations such as windowed computations, joins, and aggregations. State stores can be persistent or in-memory, and they can be queried interactively.
Topic Cleanup Policies: compact, delete
Kafka topics can have different cleanup policies that determine how old data is discarded. The two primary policies are:
- Compact: This policy retains at least the last known value for each key. It's beneficial for log compaction and reducing storage requirements while maintaining data completeness for keys.
- Delete: This policy purges records after a specified retention time or size has been reached.
These cleanup policies are crucial for managing the storage and the lifecycle of records in Kafka Topics.
Combining Cleanup Policies for Kafka Streams State Stores
State stores in Kafka Streams typically use Kafka topics (changelogs) internally to back their state. This ensures fault-tolerance by logging state changes to Kafka itself. Given the nature of stateful operations, these topics often have different characteristics compared to regular Kafka topics.
The question of using a compact,delete policy for state store topics arises when one wants both compaction (to save space and retain state consistency) and deletion (to limit historical state data and manage storage costs).
Effectively, to apply a compact,delete policy, you would set up the state store to use a Kafka topic as a backend with such a policy. However, this is where things can get tricky with Kafka Streams:
- Kafka Streams and Topic Configuration: By default, the internal topics that Kafka Streams uses for state stores have a cleanup policy of
compactto ensure that state recovery is possible and complete. This is essential for stateful applications to recover the latest state of each key after a failure. - Custom Cleanup Policy: While Kafka itself supports the
compact,deletepolicy on topics, using it for Kafka Streams state store backends is discouraged. Applying a deletion policy could mean losing essential state data necessary for correct application behavior on restoration or scaling operations.
Technical Example
Consider a simple Kafka Streams application where a state store is used to count words. To implement it with specific topic configurations, you might do something like:
This code tries to set the compact,delete policy via withLoggingEnabled. However, doing this could lead to inadvertent data loss.
Conclusion and Best Practices
Using a compact,delete policy on Kafka Streams state stores can have unintended side effects, potentially compromising the integrity and completeness of state data. It is generally recommended to use the default compaction policy for state stores and manage storage through other means, such as appropriately sizing state stores and using efficient data structures and serialization formats.
Summary Table for compact,delete Policy in Kafka Streams State Stores
| Aspect | Detail |
| Default Cleanup Policy | compact for state stores |
| Custom Policy Support | Possible but risky |
| Risk | Data loss, incomplete state recovery |
| Recommended Usage | Avoid for state stores; suitable for non-critical, transient data topics |
This article highlights the nuanced considerations needed when dealing with Kafka Streams state stores and emphasizes the importance of understanding the implications of various topic configurations for robust stream-processing applications.
Related reading
- Kafka Streams Kafka Streams application stuck rebalancing
- Kafka Streams KTable configuration error on Message Hub
- Kafka Streams KTable store with change log topic vs log compacted source topic
- Kafka Streams one record to multiple records
- Kafka Streams Persistent Store cleanup
- Kafka Streams processor API context.forward
- Kafka Streams processors - state store and input topic partitioning
- Kafka Streams Proper way to exit on error

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.