0
0
Kafkadevops~10 mins

Windowed operations in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Windowed operations
Stream of events
Assign event timestamps
Define window size & type
Group events into windows
Aggregate or process events per window
Emit windowed results
Slide or advance window
Back to Group events into windows
Events flow in with timestamps, get grouped into time windows, processed, and results are emitted per window.
Execution Sample
Kafka
KStream<String, Integer> stream = builder.stream("input-topic");
KTable<Windowed<String>, Long> windowedCounts = stream
  .groupByKey()
  .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofSeconds(10)))
  .count();
This code counts events per key in 10-second tumbling windows.
Process Table
StepEvent TimestampWindow AssignedKeyActionWindowed Count
100:00:03[00:00:00 - 00:00:10)AAdd event to windowA:1
200:00:07[00:00:00 - 00:00:10)AAdd event to windowA:2
300:00:12[00:00:10 - 00:00:20)AAdd event to new windowA:1
400:00:15[00:00:10 - 00:00:20)BAdd event to windowB:1
500:00:21[00:00:20 - 00:00:30)AAdd event to new windowA:1
600:00:25[00:00:20 - 00:00:30)AAdd event to windowA:2
7Window [00:00:00 - 00:00:10) closes--Emit count for windowA:2
8Window [00:00:10 - 00:00:20) closes--Emit count for windowA:1, B:1
9Window [00:00:20 - 00:00:30) closes--Emit count for windowA:2
💡 All windows processed and counts emitted after their time range ends.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
Window [00:00:00 - 00:00:10) count for A01222222
Window [00:00:10 - 00:00:20) count for A00011111
Window [00:00:10 - 00:00:20) count for B00001111
Window [00:00:20 - 00:00:30) count for A00000122
Key Moments - 3 Insights
Why does the event at 00:00:12 start a new window instead of joining the first?
Because the first window covers [00:00:00 - 00:00:10), and 00:00:12 is outside that range, so it belongs to the next window [00:00:10 - 00:00:20). See execution_table rows 3 and 1.
Why are counts emitted only after the window closes?
Windowed operations emit results after the window time range ends to ensure all events in that window are counted. This is shown in execution_table rows 7-9.
What happens if events arrive late for a closed window?
In this example, no grace period is set, so late events are ignored or dropped. This is why windows close and emit counts strictly after their time range.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the window assigned to the event with key 'B'?
A[00:00:00 - 00:00:10)
B[00:00:20 - 00:00:30)
C[00:00:10 - 00:00:20)
DNo window assigned yet
💡 Hint
Check the 'Window Assigned' column at step 4 in the execution_table.
At which step does the count for key 'A' in the first window reach 2?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at the 'Windowed Count' column for key 'A' in execution_table rows 1 and 2.
If the window size was changed to 5 seconds, how would the window assigned at step 3 change?
AIt would be [00:00:10 - 00:00:15)
BIt would remain [00:00:10 - 00:00:20)
CIt would be [00:00:05 - 00:00:10)
DNo window would be assigned
💡 Hint
Window size defines the time range; smaller window means shorter intervals as seen in concept_flow.
Concept Snapshot
Windowed operations group streaming events by time intervals.
Define window size (e.g., 10 seconds) and type (tumbling, hopping).
Events are assigned to windows based on timestamps.
Aggregations (like count) happen per window.
Results emit after window closes.
Late events may be dropped or handled with grace period.
Full Transcript
Windowed operations in Kafka Streams process events grouped by time windows. Events flow in with timestamps and are assigned to windows based on these timestamps. For example, a 10-second tumbling window groups events from 0 to 10 seconds, then 10 to 20 seconds, and so on. Each event is added to its window's aggregation, such as counting occurrences per key. Counts are emitted only after the window time ends to ensure completeness. If events arrive late, they may be ignored if no grace period is set. This process repeats as new events arrive and windows slide forward.