liveBook · Manning

Sliding window

As explained in Kafka Streams in Action, Second Edition

A sliding window is a fixed-duration stream-processing window whose boundaries are determined by record timestamps and whose contents change as records enter or leave.

A sliding window continuously analyzes a fixed-duration portion of an event stream. Record timestamps determine each window’s start and end, while the configured maximum time difference fixes its size. section 9.1.4

Use these rules to distinguish sliding windows from other Kafka Streams windows:

  • Each incoming record creates a new window and looks back for records whose timestamps fall within the configured time difference. section 9.1.4
  • Window start and end times are inclusive, unlike the end boundary of hopping and tumbling windows. section 9.1.4
  • Windows evaluate when records enter or leave their contents, rather than on a fixed recurring schedule. section 9.1.4
  • Unlike epoch-aligned time windows, sliding-window boundaries follow event timestamps. section 9.1.5

Page-view aggregation

A Kafka Streams application can aggregate page views in 30-second sliding windows: KStream pageViewStream = builder.stream("page-view", Consumed.with(serdeString, pageViewSerde)); pageViewStream.groupByKey() .windowedBy(SlidingWindows.ofTimeDifferenceWithNoGrace(Duration.ofSeconds(30))) .aggregate(HashMap::new, pageViewAggregator) .toStream() .to("page-view-sliding-aggregates", Produced.with(windowedSerdes, pageViewAggregationSerde)); The application emits an updated aggregate as records change the window contents. section 9.1.4 section 9.7

Figure 9.16 Sliding windows starting and ending times are inclusive and look back for events occurring within the defined time difference.
figure

Trace how each incoming record establishes an inclusive window and looks back for nearby timestamped events. section 9.1.4

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains timestamp boundaries, inclusivity, look-back inclusion, evaluation triggers, and Kafka Streams implementation.
Examples
Demonstrates page-view aggregation using a 30-second SlidingWindows configuration and a downstream aggregate topic.
What you can do after
Apply it via Kafka Streams using SlidingWindows.ofTimeDifferenceWithNoGrace and a windowed aggregation topology.
How it connects
Connects Kafka Streams, timestamps, stream time, aggregation, hopping windows, tumbling windows, and session windows.

Also explained in

Compare how 5 books explain sliding window →

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.