Quick revision
The short answer
The aggregation's store was never named, so Streams generated its name from an internal operator counter. The new filter() took one number, the aggregation moved from 0000000004 to 0000000005, and Streams created a new, empty store and changelog, leaving the old one orphaned. The fix is to name every stateful element and internal topic explicitly, review topology.describe() on every change, plan real state changes as migrations, and delete orphaned internal topics.
What to remember
In this answer, we saw that Streams generates names for unnamed operators, stores and internal topics from a counter, so adding a filter shifted the aggregation's number, created a new empty store and changelog, and orphaned the old one. We fixed it by naming every stateful element with Materialized, Grouped, StreamJoined, Repartitioned and Named, reviewing topology.describe() in code review, using the newer setting that rejects generated names where available, planning real state changes as parallel migrations or resets, and deleting orphaned topics.
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.
The aggregation's store was never named, so Streams generated its name from an internal operator counter. The new filter() took one number, the aggregation moved from 0000000004 to 0000000005, and Streams created a new, empty store and changelog, leaving the old one orphaned. The fix is to name every stateful element and internal topic explicitly, review topology.describe() on every change, plan real state changes as migrations, and delete orphaned internal topics.
Why this question is asked
This bug is easy to cause and alarming to see: a harmless change to a topology wipes the application's state, with no error at all. It catches teams because the generated names look like implementation details that nobody should care about. Interviewers ask it to see whether we understand how Streams names internal resources and how to evolve a stateful topology safely. In this answer, we will look at how names are generated, reproduce what happened, fix the code, add review habits, and plan the changes that really do alter state.
1How Streams Names Things It Was Not Told to Name
Every operator in a topology, a source, a filter, an aggregation, needs a name, and every state store and internal topic needs one too. When we do not give one, Streams generates it from the operator type and a counter that increases as the builder adds nodes: KSTREAM-SOURCE-0000000000, KSTREAM-FILTER-0000000001 and so on. A store for an aggregation gets a name like KSTREAM-AGGREGATE-STATE-STORE-0000000004, and its changelog is <application.id>-KSTREAM-AGGREGATE-STATE-STORE-0000000004-changelog.
The counter depends on the position of each operator in the code. Nothing about it is tied to the meaning of the store.
2Reproducing What Happened
Here is the topology before and after, with the generated names in comments.
| Element | Before | After |
|---|---|---|
| Filter | (none) | KSTREAM-FILTER-0000000001 |
| Key select | ...KEY-SELECT-0000000001 | ...KEY-SELECT-0000000002 |
| Count store | ...STATE-STORE-0000000004 | ...STATE-STORE-0000000005 |
| Changelog | ...0000000004-changelog (orphaned) | ...0000000005-changelog (empty) |
The aggregation is the same logic, but its store name changed. On startup Streams looked for a store called ...0000000005, found none locally and no changelog with that name, created both empty, and started counting from zero. The old changelog still exists, but nothing writes to it any more. The repartition topic was probably renamed in the same way.
3Naming Everything That Holds State
The fix is to give explicit names to every element that owns state or creates a topic: stores through Materialized.as, groupings through Grouped.as, stream-stream joins through StreamJoined.withStoreName, repartitions through Repartitioned.as, and processors through Named.as. Named elements keep their names however many steps we add elsewhere.
Note that adding these names to an application that already runs is itself a rename: the first deploy with names creates new stores. So we either add names as part of a planned migration, or, if the generated names must be preserved, keep them for now and name all new elements from the start.
4Reviewing Topology Changes
topology.describe() prints every subtopology, processor, store and topic. Printing it in a test and storing the output in the repository turns any change in internal names into a visible diff in code review, which is how this incident would have been caught before deploy. A test can also fail if any store or repartition name contains a generated number.
Recent Kafka versions add a configuration (from KIP-1111) that makes Streams refuse to start when internal topics would use generated names; check whether your version offers it, because it enforces the rule without relying on reviewers.
5Changes That Really Alter State
Some changes should create new state: a different grouping key, a new serde for the store, or new aggregation logic. Those need a migration rather than a rolling deploy. The cleanest approach is to deploy the new version under a new application.id, which gets its own consumer group and its own internal topics and rebuilds state from the input topics, provided their retention covers the needed history. We run both side by side, compare their outputs, and switch consumers to the new output once they match.
If the history is not available, or we deliberately want to reprocess, kafka-streams-application-reset.sh resets the application's input offsets and deletes its internal topics, and KafkaStreams.cleanUp() removes the local state directory before the next start.
- Diff topology.describe() against production
- Do store or topic names change?YesTreat it as a state migrationNoRolling deploy
- Run the new application.id in parallelRebuild state from the input topics
- Outputs match?YesSwitch consumers to the new outputNoInvestigate before switching
- Delete the orphaned internal topics
5 steps in order. Play them, or step through them one at a time.
6Cleaning Up
Streams never deletes internal topics it no longer uses. The old ...0000000004-changelog and repartition topic keep their data and disk until we remove them. Once we are sure we will not roll back, we delete them with kafka-topics.sh --delete, or as part of the reset tool's run.
7Follow-up questions
Could we recover the old counts instead of starting from zero?
Possibly. If we roll back to the old topology, it finds its old store and changelog again and continues, though it misses whatever the new version processed. Alternatively, we can migrate by naming the store explicitly and rebuilding it from the input topics in a new application id, which is safer than trying to copy changelog data between topic names.
Does naming processors with Named.as matter, or only stores?
Stores and internal topics matter most, because they hold data. Processor names matter for readability of describe() output and metrics, and in some cases they feed into generated store names, so naming them is a cheap habit.
Why does Streams not just keep using the old name?
It has no way to know that the new 0000000005 store is the same logical store as the old 0000000004. Names are the only identity a store has, which is exactly why we should choose them ourselves.
8Conclusion
In this answer, we saw that Streams generates names for unnamed operators, stores and internal topics from a counter, so adding a filter shifted the aggregation's number, created a new empty store and changelog, and orphaned the old one. We fixed it by naming every stateful element with Materialized, Grouped, StreamJoined, Repartitioned and Named, reviewing topology.describe() in code review, using the newer setting that rejects generated names where available, planning real state changes as parallel migrations or resets, and deleting orphaned topics.
Open with
The aggregation's store was not named, so Streams generated its name from an operator counter, KSTREAM-AGGREGATE-STATE-STORE plus a number based on the operator's position. The new filter took one number earlier in the chain, so the aggregation moved from 4 to 5.
Then, if they want more
On startup Streams found no store or changelog with the new name, created them empty and counted from zero, while the old changelog stopped receiving data.
To prevent it, I name everything that holds state or creates topics: Materialized.as for stores, Grouped.as for groupings and their repartition topics, StreamJoined.withStoreName for joins, and Named.as for processors, so unrelated steps never rename them.
In code review I compare topology.describe() with the production version, ideally as a stored test snapshot, and in recent versions there is a setting to reject generated names for internal topics, so I would check whether ours has it.
For changes that genuinely alter state, a new key, a new serde or new logic, I plan a migration: run a new application id in parallel that rebuilds from the input topics if retention allows, compare outputs, then switch, or reprocess with the application reset tool and cleanUp. Afterwards I delete the orphaned internal topics, because Streams leaves them behind.
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 "Afterwards the per-customer counts restarted from zero, a new internal topic ending in...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/61c9884a4d/