Quick revision
The short answer
Each instance sets application.server to its own host and port so Streams shares every instance's endpoint, and the aggregation uses a fixed store name. The controller asks kafkaStreams.queryMetadataForKey() which instance owns the customer: if it is this one, it reads the local store; otherwise it forwards the HTTP call to the owner. During a rebalance or restore the store may be unavailable or the metadata stale, so the endpoint returns 503 with Retry-After, or answers from a standby with enableStaleStores() if slightly stale data is acceptable.
What to remember
In this answer, we saw that interactive queries need application.server on every instance and a fixed store name, so that queryMetadataForKey can tell which instance owns a customer. The controller reads the local store when it owns the key and forwards otherwise, guarding against forwarding loops. During rebalances and restores it returns 503 with Retry-After instead of wrong answers, can serve slightly stale data from standbys with enableStaleStores(), and clients must accept that answers are eventually consistent with the input topic.
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 instance sets application.server to its own host and port so Streams shares every instance's endpoint, and the aggregation uses a fixed store name. The controller asks kafkaStreams.queryMetadataForKey() which instance owns the customer: if it is this one, it reads the local store; otherwise it forwards the HTTP call to the owner. During a rebalance or restore the store may be unavailable or the metadata stale, so the endpoint returns 503 with Retry-After, or answers from a standby with enableStaleStores() if slightly stale data is acceptable.
Why this question is asked
Interactive queries let a Kafka Streams application serve its state directly, without copying it into a database. That is attractive, but the state is split across 6 instances, and it moves around during rebalances. Interviewers ask this to see whether we can route queries correctly and handle the moments when state is not available. In this answer, we will recap why state is partitioned, configure the instances, write the controller, handle rebalances and restores, and look at stale reads and consistency.
1Why Any Instance Cannot Simply Read Its Store
The running total per customer is kept in order-totals-store, but each of the 6 instances holds only the tasks, and therefore the partitions of the store, assigned to it. Customer c-42's total lives in the store of whichever task owns c-42's partition. If a request lands on another instance, a local lookup returns nothing, which looks exactly like a customer with no orders.
So every instance needs two things: a way to find out which instance owns a key, and a way to ask that instance.
2The Settings That Make Routing Possible
application.server is a host:port that each instance advertises in the consumer group metadata. Streams shares these endpoints among all members, so any instance can map a key to the host that owns it. The store must also have a fixed name, both for querying and so it does not change when the topology changes.
3Writing the Endpoint
In Spring Boot we get the KafkaStreams instance from the StreamsBuilderFactoryBean once it has started. The controller then asks for the key's metadata and either reads locally or forwards.
7 messages in order. Play them, or step through them one at a time.
Notice that the forwarded request goes to the same endpoint on the owner. In production we mark forwarded requests with a header and make the receiver answer only locally, so two instances with different views of the metadata cannot bounce a request between them forever.
4What the Endpoint Does During a Rebalance
Rebalances move tasks between instances, and restoration can take a while after a move. In that window two things go wrong. The local store may not be queryable, and store() or the read throws InvalidStateStoreException. Or the metadata still points at an instance that has just lost the task. The endpoint must not turn either into a wrong answer such as 404 or zero.
The safe behaviour is to return 503 with a short Retry-After, or retry internally with backoff for a second or two, which hides brief rebalances from clients. Clients should treat 503 as retriable.
5Stale Reads and Consistency
If availability matters more than freshness, StoreQueryParameters.enableStaleStores() allows reading from a standby replica or a store that is still restoring. KeyQueryMetadata.standbyHosts() lists where the standbys are, so a request can fall back to one of them when the active host is down. The answer may be a little behind, which is often acceptable for a running total.
Even the active store is only eventually consistent with the input topic: an order placed a moment ago may still be in the topic or in the repartition step. A client that just placed an order may not see it yet. If read-your-writes matters, the UI can add the new order locally, or the API can use the newer query API that reports how far each partition has processed.
6Follow-up questions
What does the Interactive Queries v2 API (StateQueryRequest) add?
IQv2 replaces the store() call with kafkaStreams.query(StateQueryRequest.inStore("order-totals-store").withQuery(KeyQuery.withKey(id))). The result reports results per partition along with the position, the input offsets each partition has processed, and a request can set a position bound so it only returns data at least that fresh, which supports read-your-writes. It also lets us target specific partitions and get typed queries such as range and window queries. It is still evolving and some query types are marked as evolving, so check what your Kafka version supports.
Why not copy the totals into a database instead?
That is often the better choice when many services need the data, when queries are richer than key lookups, or when the team does not want to run routing logic. Interactive queries suit a service that owns its data and needs fast key lookups without another system.
How do you build HostInfo for the current instance?
From the same value we put in application.server, for example new HostInfo(System.getenv("POD_IP"), 8080), so the comparison with activeHost() matches exactly.
7Conclusion
In this answer, we saw that interactive queries need application.server on every instance and a fixed store name, so that queryMetadataForKey can tell which instance owns a customer. The controller reads the local store when it owns the key and forwards otherwise, guarding against forwarding loops. During rebalances and restores it returns 503 with Retry-After instead of wrong answers, can serve slightly stale data from standbys with enableStaleStores(), and clients must accept that answers are eventually consistent with the input topic.
Open with
The state is partitioned, so each of the six instances holds only some customers' totals. First, each instance sets application.server to its own host and port, which Streams shares through the group metadata, and the aggregation is materialized with a fixed name, Materialized.as order-totals-store.
Then, if they want more
In Spring Boot I get the KafkaStreams object from StreamsBuilderFactoryBean.getKafkaStreams once it is running. In the controller I call queryMetadataForKey with the store name, the customer id and the String serializer.
If the active host is this instance, I read with kafkaStreams.store using StoreQueryParameters.fromNameAndType and the key-value store type, then get the customer id; otherwise I forward the HTTP call to the active host, marking it as forwarded so it cannot loop.
During a rebalance or restore, the store can throw InvalidStateStoreException or the metadata can point at an instance that just lost the task, so I return 503 with Retry-After, or retry briefly, never a 404 or zero. If availability matters more, enableStaleStores lets me answer from a standby, and KeyQueryMetadata.standbyHosts tells me where they are.
Answers are eventually consistent with the input, so a just-placed order may not show yet.
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 "Explain how you implement it so any instance can answer for any customer, which settings...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/c26ae7bce7/