A session window groups records by activity rather than by a fixed duration. Events extend the window while each new event arrives within the configured inactivity gap; after a longer gap, later events begin a new session. section 9.1.3 section 9.1.2
A Kafka Streams session-window aggregation typically combines these parts:
- Define the window with SessionWindow.ofInactivityGapWithNoGrace; its duration parameter specifies inactivity before closure, not a fixed window size. section 9.1.3
- Group records and aggregate them with an Aggregator, such as counting each URL visited by a customer. section 9.1.3
- Provide a Merger because adjacent sessions can become one session when arriving activity bridges their gap. section 9.1.3
- Use a session-windowed Serde when writing windowed results to an output topic. section 9.1.3
Tracking customer pageviews
A page-view stream can group records by customer, apply SessionWindow.ofInactivityGapWithNoGrace(Duration.ofMinutes(2)), aggregate URL counts, merge session maps, and write results to page-view-session-aggregates. The first event timestamp sets the session start; the last event before the gap sets its end. section 9.1.5
Figure 9.13 Session windows continue to grow in size up to an activity gap, and then a new session starts.
Visualizes sessions growing with incoming events until inactivity exceeds the configured gap, after which a new session begins. section 9.1.3
Figure 9.14 Merging session windows combines two sessions into one.
Shows how two session windows combine into one aggregate when session merging occurs. section 9.1.3
