Kafka Streams
State Stores
Data Policy
Compact Delete Policy
Data Management

Kafka Streams Is it possible to have compact,delete policy on state stores?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

  1. Kafka Streams and Topic Configuration: By default, the internal topics that Kafka Streams uses for state stores have a cleanup policy of compact to 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.
  2. Custom Cleanup Policy: While Kafka itself supports the compact,delete policy 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:

java
1StreamsBuilder builder = new StreamsBuilder();
2KTable<String, Long> wordCounts = builder.table(
3    "input-topic",
4    Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("word-counts-store")
5        .withLoggingEnabled(Collections.singletonMap("cleanup.policy", "compact,delete"))
6);
7
8wordCounts.toStream().to("output-topic");
9KafkaStreams streams = new KafkaStreams(builder.build(), new StreamsConfig(properties));
10streams.start();

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

AspectDetail
Default Cleanup Policycompact for state stores
Custom Policy SupportPossible but risky
RiskData loss, incomplete state recovery
Recommended UsageAvoid 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.


Course illustration
Course illustration

All Rights Reserved.