Kafka Streams
Persistent Store
Data Management
Stream Processing
Cleanup Process

Kafka Streams Persistent Store cleanup

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka clusters. It combines the simplicity of building and deploying standard Java and Scala applications on the client side with the benefits of Kafka's server-side cluster technology.

Understanding Kafka Streams Persistent Store Cleanup

Persistent state stores in Kafka Streams are critical for maintaining state derived from stream processing. These are typically managed in local storage using RocksDB or an in-memory hash map. However, over time, data may accumulate, leading to increased storage usage or overdue cleanup tasks. Managing this data lifecycle is imperative for efficient performance.

Importance of Cleanup

Proper cleanup of state stores ensures:

  • Efficient use of storage.
  • Better recovery times in case of rebalances or application restarts.
  • Reduced memory footprint, leading to improved application performance.

Methods of Cleanup

  1. Retention Period Configuration Kafka Streams allows you to configure retention periods for your state stores when using windowed aggregations. Records outside this retention period are not accessible and are purged.
    Configuration example:
java
1   StoreBuilder<WindowStore<String, Long>> storeBuilder = Stores.windowStoreBuilder(
2       Stores.persistentWindowStore(
3           "my-store", 
4           Duration.ofDays(1), 
5           Duration.ofMinutes(5), 
6           false
7       ),
8       Serdes.String(), 
9       Serdes.Long()
10   );
  1. Log Compaction Kafka itself supports log compaction on topics. Kafka Streams utilizes this feature for its changelog topics, ensuring that state stores do not grow indefinitely.
  2. Manual Cleanup
    • Programmatic cleanup: Kafka Streams API supports manually triggering cleanup through the state store’s API.
    • External tools/scripts: Teams may build automation scripts to clean records periodically outside of Kafka Streams.
  3. On-Rebalance Cleanup Handlers Kafka Streams supports automatic cleanup of state stores when a stream task is closed or rebalanced. onPartitionRevoked and onPartitionAssigned can be used to manage cleanup.
  4. TTL for RocksDB For state stores backed by RocksDB, you can enable Time-To-Live (TTL) settings to automatically purge old records.

Example: Setting TTL for RocksDB

java
1// Suppose you have configured your Kafka Streams application as follows:
2StreamsBuilder builder = new StreamsBuilder();
3KTable<String, String> table = builder.table("input-topic", 
4    Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("store-name")
5        .withKeySerde(Serdes.String())
6        .withValueSerde(Serdes.String())
7        .withLoggingEnabled(Collections.emptyMap())  // disable changelogging for this example
8);
9
10StreamsConfig props = new StreamsConfig(getProperties());
11
12// To add TTL to the RocksDB state store:
13RocksDBConfigSetter setter = (options, conf) -> {
14    options.setTtl(86400 * 1000); // TTL set for 24 hours
15};
16
17props.put(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, setter);
18KafkaStreams streams = new KafkaStreams(builder.build(), props);
19
20streams.start();

Table: Summary of Cleanup Techniques and Their Impacts

MethodDescriptionImpact
Retention PeriodSets the maximum life for records in windowed state stores.Auto-purge old data. Reduces storage needs, lowers recovery times.
Log CompactionKafka topic compaction feature.Prevents indefinite data growth, ensures only latest value for each key is maintained.
Manual CleanupProgrammatic or external script-based cleanup.Allows precise control over cleanup activities, useful for ad-hoc maintenance needs.
On-Rebalance Cleanup HandlersUtilize stream task lifecycle events for cleanup.Enhances manageability during topology changes, useful in dynamic scaling scenarios.
TTL for RocksDBTime-To-Live for records in RocksDB.Automatic record expiration, easier management of data lifecycle.

Advanced Considerations

  • Regular monitoring of state store sizes can preempt performance issues.
  • Strategies for state migration and backup during large-scale cleanups are crucial to avoid data loss.
  • Combining TTL with log compaction can optimize both read performance and storage efficiency.

In conclusion, managing persistent store cleanup in Kafka Streams is crucial for maintaining application efficiency and performance. By leveraging Kafka’s inherent features along with careful configuration and management practices, developers can ensure their stream-processing applications run smoothly and scale effectively.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.