0
0
Kafkadevops~10 mins

Interactive queries in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Interactive queries
Start Kafka Streams App
Process Streams & Update Local State
Expose Local State Store
Receive Query Request
Check Local Store for Data
Return Data
Respond to Client with Data
End
The app processes streams and updates local state. When a query comes, it checks local data. If found, returns it; otherwise, asks other instances.
Execution Sample
Kafka
StreamsBuilder builder = new StreamsBuilder();
KTable<String, Long> counts = builder.table("input-topic", Materialized.<String, Long>as("counts-store"));
KafkaStreams streams = new KafkaStreams(builder.build(), props);
streams.start();
InteractiveQueryService iqService = streams.interactiveQueryService();
ReadOnlyKeyValueStore<String, Long> store = iqService.getQueryableStore("counts-store", QueryableStoreTypes.keyValueStore());
Long value = store.get("key1");
This code builds a stream table, gets the interactive query service, accesses the local state store, and queries a value by key.
Process Table
StepActionEvaluationResult
1Start Kafka Streams appApp initializes and starts processingStreams app running
2Process input topic and update local state storeRecords processed and counts updatedLocal store 'counts-store' updated
3Receive query for key 'key1'Check local store for 'key1'Key found locally? Yes
4Return value for 'key1' from local storeValue retrievedValue = 42
5Respond to client with valueSend responseClient receives value 42
6Query for key 'keyX' not in local storeCheck local store for 'keyX'Key found locally? No
7Forward query to remote instancesRemote query sentRemote instance returns value 100
8Respond to client with remote valueSend responseClient receives value 100
9EndAll queries handledApp continues running
💡 Queries stop when data is found locally or remotely; app keeps running.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 7Final
streamsnullinitializedrunningrunningrunning
counts-storeemptyupdated with keyscontains key1=42contains key1=42, keyX unknowncontains key1=42, keyX unknown
query keynonenonekey1keyXnone
query resultnonenone42100 (from remote)none
Key Moments - 3 Insights
Why does the app check the local store first before asking other instances?
Because checking local store is faster and reduces network calls. See execution_table rows 3 and 6 where local check decides next step.
What happens if the key is not found locally or remotely?
The query returns null or empty result. The app tries remote queries after local miss (row 7), but if no instance has data, client gets no data.
How does the app keep the local state updated?
By continuously processing the input topic and updating the local store (row 2). This keeps data fresh for queries.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value returned for key 'key1' at step 4?
A100
B42
Cnull
Dkey1
💡 Hint
Check row 4 in execution_table where local store returns value 42.
At which step does the app forward the query to remote instances?
AStep 3
BStep 6
CStep 7
DStep 5
💡 Hint
Look at execution_table row 7 where remote query is sent after local miss.
If the local store had the key 'keyX', how would the execution_table change?
AStep 6 would return value locally, skipping steps 7 and 8
BStep 7 would still forward query to remote
CStep 4 would return null
DStep 3 would forward query to remote
💡 Hint
Refer to variable_tracker and execution_table rows 3-8 about local vs remote query handling.
Concept Snapshot
Interactive Queries in Kafka Streams:
- Streams app maintains local state stores.
- Queries first check local store for data.
- If data missing locally, query forwards to remote instances.
- Enables fast, scalable state access in distributed apps.
- Use InteractiveQueryService to access stores programmatically.
Full Transcript
This visual execution trace shows how Kafka Streams interactive queries work. The app starts and processes streams, updating a local state store. When a query arrives, it first checks the local store. If the key is found, it returns the value immediately. If not found, it forwards the query to other instances to find the data remotely. The app responds to the client with the found value. Variables like the streams app state, local store contents, query keys, and results change step-by-step. Key moments clarify why local checks happen first, what if data is missing, and how state updates. The quiz tests understanding of values returned, query forwarding steps, and how local data presence affects flow. The snapshot summarizes the concept for quick recall.