What are the four window types in Kafka Streams, and how do they differ?
answer
- Tumbling = non-overlapping buckets
- Hopping = size + advanceBy (overlap)
- Sliding = ofTimeDifferenceAndGrace (pairwise)
- Session = inactivity gap, merges
- Tumbling is hopping with advance==size
basics
~10 sTumbling (fixed, non-overlapping), hopping (fixed size, overlapping by advance interval), sliding (window around record pairs within a time difference), and session (activity-gap-based, dynamic size). The first three are time-based; sessions are data-driven.
solid answer
~40 sKafka Streams has four window types. Tumbling windows are fixed-size and non-overlapping, so each record falls into exactly one window (e.g. 1-minute buckets). Hopping windows are fixed-size but advance by a smaller hop, so they overlap and a record can land in multiple windows (e.g. 5-min size, 1-min advance). Sliding windows (SlidingWindows.ofTimeDifferenceAndGrace) create windows based on record timestamps so two records are in the same window only if their timestamps differ by at most the configured size — used mainly for sliding aggregations and joins. Session windows (SessionWindows) are data-driven: a session grows as records arrive and closes after an inactivity gap; adjacent sessions merge. Tumbling is a special case of hopping where advance == size. All use TimeWindows/SlidingWindows/SessionWindows builders.
go deeper
Be able to name the four types and give a one-line distinction (overlap vs not, fixed vs variable).
Know the builder classes (TimeWindows, SlidingWindows, SessionWindows) and that tumbling == hopping with advance==size.
Explain time-based vs data-driven boundaries, why sliding beats tiny hopping windows, and session merging on out-of-order data.
Reason about state/output amplification from overlapping windows and choose window type by workload (billing buckets vs user sessions vs anomaly detection).
## What is windowing? A **window** groups records by time so you can compute aggregates over bounded slices of an unbounded stream (counts, sums, etc.). Without windows, an aggregation over a stream would accumulate forever. Windowing turns "count all clicks" into "count clicks per minute". Kafka Streams offers **four** window types: ### 1. Tumbling windows Fixed size, **non-overlapping**, gapless. Each record belongs to **exactly one** window. A 1-minute tumbling window produces buckets [00:00,00:01), [00:01,00:02), … Built with `TimeWindows.ofSizeAndGrace(Duration.ofMinutes(1), grace)`. ### 2. Hopping windows Fixed **size** but **advance** (hop) by a smaller interval, so windows **overlap**. A 5-minute window advancing every 1 minute means each record falls into up to 5 windows simultaneously. Built with `TimeWindows.ofSizeAndGrace(size, grace).advanceBy(advanceInterval)`. **Tumbling is just hopping where advance == size** (the default when you don't call `advanceBy`). ### 3. Sliding windows `SlidingWindows.ofTimeDifferenceAndGrace(maxDiff, grace)`. Windows are defined **relative to record timestamps**: two records are aggregated together only if their timestamps differ by at most `maxDiff`. New windows are created at record boundaries, so windows are as numerous as needed but each is exactly `maxDiff` wide. Far more efficient than emulating the same behavior with tiny hopping windows. Used for sliding aggregations (added in KIP-450). ### 4. Session windows `SessionWindows.ofInactivityGapAndGrace(gap, grace)`. **Data-driven**, **variable size**. A session keeps growing as long as records keep arriving within `gap` of each other; when a gap larger than `inactivityGap` occurs, the session closes. Out-of-order records can cause two existing sessions to **merge** into one. Great for user-activity / clickstream analysis. ## Time-based vs data-driven Tumbling, hopping, and sliding windows have boundaries derived purely from the clock/record timestamps and a fixed size. Session windows have boundaries derived from the **data distribution** (where the gaps are), so two different keys can have completely different session boundaries. ## Edge cases - A record's window membership is decided by its **event-time timestamp** (the timestamp extractor / `TimestampExtractor`), not wall-clock arrival. - Overlapping windows (hopping/sliding) multiply the amount of state and the number of output rows per record. - Session windows are the only type where windows can shrink/grow/merge retroactively as late data arrives within grace.
- How is a tumbling window related to a hopping window in the API?A tumbling window is a hopping window where the advance interval equals the window size. If you build a TimeWindows and never call advanceBy(), you get a tumbling window by default.
- Which window type produces variable-length windows and why?Session windows. Their boundaries are data-driven by inactivity gaps, so each session lasts as long as records keep arriving within the gap; late out-of-order records can even merge two sessions.
saying these in an interview costs you the question
- Saying tumbling windows can overlap (they cannot — that's hopping)
- Claiming all window types are fixed size (sessions are variable)
- Confusing sliding windows with hopping windows of small advance
- Saying window membership is decided by arrival/wall-clock time rather than event-time timestamp