0
0
Kafkadevops~10 mins

State stores in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - State stores
Start Kafka Streams App
Create State Store
Process Incoming Records
Update State Store
Query State Store
Emit Results or Continue Processing
End
This flow shows how a Kafka Streams app creates a state store, updates it with incoming data, and allows querying or further processing.
Execution Sample
Kafka
store = builder.getStateStore("counts")
for record in input:
    current = store.get(record.key) or 0
    store.put(record.key, current + 1)
    output(record.key, current + 1)
Counts occurrences of keys from input records by updating a state store and outputting the count.
Process Table
StepInput Record KeyCurrent Count from StoreActionUpdated Store ValueOutput
1applenullGet count (null), set to 0 + 11apple: 1
2banananullGet count (null), set to 0 + 11banana: 1
3apple1Get count (1), increment by 12apple: 2
4apple2Get count (2), increment by 13apple: 3
5banana1Get count (1), increment by 12banana: 2
6cherrynullGet count (null), set to 0 + 11cherry: 1
7apple3Get count (3), increment by 14apple: 4
Exit--No more records--
💡 All input records processed, no more data to update state store.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
store['apple']null11233344
store['banana']nullnull1112222
store['cherry']nullnullnullnullnullnull111
Key Moments - 3 Insights
Why do we check if the current count is null before incrementing?
Because the state store may not have a value for a key yet (null means no count), so we start counting from zero. See execution_table step 1 where 'apple' count is null and set to 1.
Does the state store update immediately after each record?
Yes, after processing each record, the store is updated with the new count. For example, after step 3, 'apple' count is updated from 1 to 2.
What happens if we query the store for a key not seen before?
The store returns null, meaning no data yet. We then treat it as zero count before incrementing. See steps with 'cherry' in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the store value for 'banana' after step 5?
A2
B3
C1
Dnull
💡 Hint
Check the 'Updated Store Value' column at step 5 for 'banana'.
At which step does the store first get a value for 'cherry'?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the first time 'cherry' appears in the 'Input Record Key' column.
If the input had no 'apple' records, what would be the final store value for 'apple'?
A1
B0
Cnull
Dundefined
💡 Hint
Refer to variable_tracker for 'store["apple"]' start and final values when no updates occur.
Concept Snapshot
State stores hold data locally in Kafka Streams apps.
They keep track of information like counts or aggregates.
You read from the store, update it, then write back.
If no data exists for a key, treat it as zero or empty.
State stores enable fast, fault-tolerant stateful processing.
Full Transcript
This visual trace shows how Kafka Streams uses state stores to keep track of counts for keys. The app starts by creating a state store. For each input record, it reads the current count from the store. If no count exists, it treats it as zero. Then it increments the count and updates the store. The updated count is output. This process repeats for each record. The variable tracker shows how counts for keys like 'apple', 'banana', and 'cherry' change step-by-step. Key moments clarify why we check for null counts and how the store updates immediately. The quiz tests understanding of store values at different steps and what happens if keys are missing. Overall, state stores let Kafka Streams apps remember information across records to do stateful processing.