liveBook · Manning

Session window

As explained in Kafka Streams in Action, Second Edition

SessionWindows is a Kafka Streams class that defines session windows separated by an inactivity gap.

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.
figure

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.
figure

Shows how two session windows combine into one aggregate when session merging occurs. section 9.1.3

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains inactivity gaps, event-time boundaries, SerDes, aggregation, and merging, but does not detail state-store internals.
Examples
Demonstrates two-minute customer pageview sessions that count visited URLs and merge adjacent session aggregates.
What it assumes
Assumes familiarity with Kafka Streams aggregation and windowing while contrasting session windows with fixed-size windows.
What you can do after
Apply it via Kafka Streams to build event-time aggregations for irregular activity and serialize windowed results.
How it connects
Connects session windows with event timestamps, aggregation, SerDes, Merger, and tumbling, hopping, and sliding windows.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.