Quick revision
The short answer
Each task has its own store, RocksDB by default, under state.dir/inventory-app/<task id>/rocksdb/stock-store, and every write also goes to the compacted changelog topic inventory-app-stock-store-changelog, which is the durable copy. When a pod is deleted, its tasks move and the new owner restores each store from the changelog, from the start or from a local checkpoint, before processing. state.dir defaults to a folder under the temp directory, which in a container is usually lost on restart, so every restart becomes a full restore; mount a persistent volume and point state.dir at it.
What to remember
In this answer, we saw that every task holds its own store instance, RocksDB by default, under state.dir/<application.id>/<task id>/rocksdb/<store>, and that every write also goes to a compacted changelog topic with one partition per task. When a pod is deleted, the new owner restores each store from the changelog, from a checkpoint if it has one, and processes nothing until it finishes. The default state.dir under the temp directory is lost in containers, so persistent volumes and a configured state.dir turn full restores into short catch-ups.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each task has its own store, RocksDB by default, under state.dir/inventory-app/<task id>/rocksdb/stock-store, and every write also goes to the compacted changelog topic inventory-app-stock-store-changelog, which is the durable copy. When a pod is deleted, its tasks move and the new owner restores each store from the changelog, from the start or from a local checkpoint, before processing. state.dir defaults to a folder under the temp directory, which in a container is usually lost on restart, so every restart becomes a full restore; mount a persistent volume and point state.dir at it.
Why this question is asked
Kafka Streams applications look stateless from the outside because they have no database, yet they keep state on local disk and in Kafka. Not knowing where that state lives is the root cause of most slow restarts in containers. Interviewers ask this to check that we understand the two copies of the state and how they are kept in sync. In this answer, we will look at tasks and stores, where the local files live, how the changelog protects them, what happens when a pod disappears, and why the default state directory is a trap in containers.
1A Quick Recap of Tasks
Kafka Streams splits a topology into subtopologies, parts connected only through topics, and creates one task per input partition of each subtopology. A task is the unit of work and of state: it owns its partitions and its own instance of every state store in its subtopology. If the topology reads a 12-partition topic and groups by warehouse with a repartition, the counting subtopology has 12 tasks, named like 1_0 to 1_11 (subtopology 1, partition 0 to 11), and therefore 12 separate stock-store instances.
Because each task's store holds only the keys of its partition, tasks can move between instances independently, which is how Streams scales and fails over.
2Where the Local State Lives
Each store instance is, by default, a RocksDB database: an embedded key-value store that writes to local disk and caches hot data in memory. Its files sit in a predictable path.
Reads and writes go to this local database, so a count does not make a network call per record. That is why stateful Streams applications are fast: the state is next to the code that uses it.
3The Changelog: the Durable Copy
Local disk can be lost, so every write to the store is also sent to a compacted topic, the changelog, named <application.id>-<store name>-changelog, here inventory-app-stock-store-changelog. It has one partition per task, so task 1_3 writes to changelog partition 3. Compaction keeps at least the latest value per key, so the topic stays roughly the size of the store rather than growing with every update.
The local RocksDB is a fast cache of the changelog. The changelog is the truth that survives any instance.
- Task 1_3 processingReads and updates counts per warehouse
- RocksDB stock-store (local disk)Fast reads and writes, lost with the disk
- .checkpoint fileChangelog offset the local copy has reached
- inventory-app-stock-store-changelog, partition 3Compacted topic in Kafka, the durable copy
4 layers, from the top down. Step through them one at a time.
4What Happens When a Pod Is Deleted
When the pod goes away, the consumer group rebalances and task 1_3 is assigned to another instance. That instance has no local store for 1_3, or an old one, so before processing any new records it restores: it reads changelog partition 3 into a fresh RocksDB. If it has an older copy with a .checkpoint file, it starts reading from the offset in that file and replays only the tail. Without a checkpoint it reads from the beginning.
While restoration runs, the task processes nothing, so lag on its input partition grows. For a store of a few hundred megabytes that is seconds; for tens of gigabytes it can be many minutes. The cure is to avoid full restores, which brings us to the state directory.
- Task 1_3 assigned to a new instance
- Local store with .checkpoint?YesReplay changelog from checkpoint offsetNoReplay changelog from the beginning
- Caught up with the changelog endRestoration finished
- Start processing input partition 3Lag shrinks again
4 steps in order. Play them, or step through them one at a time.
5Why the Default state.dir Hurts in Containers
state.dir defaults to kafka-streams under the JVM temp directory, /tmp/kafka-streams on Linux. In a container, /tmp is part of the container's writable layer or an emptyDir volume, so it disappears when the container or pod is replaced. Every deploy or restart then forces a full restore of every store from the changelogs, even though the same instance was holding that state a minute earlier.
The fix is to give each instance stable storage. On Kubernetes, a StatefulSet gives every pod a stable name and its own persistent volume claim; we mount the volume and set state.dir to it, for example spring.kafka.streams.state-dir=/var/lib/kafka-streams. After a restart the pod finds its RocksDB files and checkpoint and replays only the records written while it was down.
6Follow-up questions
What does num.standby.replicas change in this picture?
With num.standby.replicas=1, Streams keeps a second, passive copy of each store on another instance, kept up to date by reading the changelog continuously. When a pod is deleted, its active tasks move to the instances holding standbys, which only need to catch up a small lag before processing. Failover becomes seconds instead of a full restore, at the cost of twice the state disk and extra changelog reads.
Are in-memory stores a way to avoid disk problems?
Stores.inMemoryKeyValueStore keeps the data on the heap and still writes a changelog, but it has nothing local to keep across restarts, so it always restores fully on start. That is fine for small state, a few tens of megabytes, and wrong for large state.
Can two instances share one state directory?
No. Streams locks the task directories, and sharing would corrupt the stores. Each instance needs its own directory, which is what one volume per StatefulSet pod gives us.
7Conclusion
In this answer, we saw that every task holds its own store instance, RocksDB by default, under state.dir/<application.id>/<task id>/rocksdb/<store>, and that every write also goes to a compacted changelog topic with one partition per task. When a pod is deleted, the new owner restores each store from the changelog, from a checkpoint if it has one, and processes nothing until it finishes. The default state.dir under the temp directory is lost in containers, so persistent volumes and a configured state.dir turn full restores into short catch-ups.
Open with
Every task, which is one per input partition of the subtopology, has its own instance of stock-store, a RocksDB database by default, under state.dir, then the application id inventory-app, then the task id like 1_3, then rocksdb/stock-store.
Then, if they want more
Reads and writes are local, so counting does not call Kafka per record. Every write is also sent to a compacted changelog topic, inventory-app-stock-store-changelog, with one partition per task, and that topic is the durable copy.
When a pod is deleted, its tasks are reassigned, and the new owner restores each store by reading the changelog partition, from the beginning, or from the offset in its .checkpoint file if it still has an older local copy. The task processes nothing until restoration finishes, so lag grows.
The problem with the default state.dir is that it is under the JVM temp directory, /tmp/kafka-streams, which in a container is usually ephemeral, so every restart throws the state away and forces a full restore.
I would run Streams as a StatefulSet with a persistent volume per pod and point state.dir at it, and add standby replicas if failover time matters. In-memory stores also restore fully on every start, so they are only for small state.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Where is this state kept on the instance and in Kafka, what happens to it when a pod is...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/f98f77ef26/