How does Kafka Streams handle state?

Kafka Streams manages application state using local, embedded key-value stores (RocksDB by default) that are kept durable by continuously backing up their changes to internal, compacted Kafka topics called changelog topics.

Key Points: • Each stateful operator (aggregations, joins, windowing) is backed by a local state store co-located with the stream task for fast, low-latency reads and writes. • Every state store has a corresponding changelog topic that records every update, so the store can be fully rebuilt if the instance restarts or moves. • State is partitioned the same way as the input topics, so each task only owns the state for the partitions it's processing. • When an instance fails and its tasks are reassigned, the new instance restores the RocksDB store by replaying the changelog topic before resuming processing.

Example: A word-count Kafka Streams application keeps its running counts in a local RocksDB store; if the instance crashes, a standby or newly assigned instance rebuilds that same store by reading the changelog topic from the beginning, then resumes counting exactly where processing left off.

Interview Tip: A concise interview answer is:

"Kafka Streams keeps state in local RocksDB stores for fast access, and durably backs every update up to an internal changelog topic, so if an instance fails, whichever instance takes over the partition can rebuild the state store from the changelog and resume processing without losing data."