Home/Learn/Apache Kafka/Kafka Streams Windowing

Kafka Streams Windowing

Advanced
Kafka Streams

Windowing groups events into time-bounded buckets for aggregation. Kafka Streams supports tumbling, hopping, sliding, and session windows for real-time analytics.

Overview

Stream processing often requires aggregating events over time — orders per minute, error rate in the last 5 minutes. Kafka Streams provides four window types. Handling late arrivals requires a grace period after which late records are discarded.

Window Types with Examples

Tumbling: non-overlapping fixed buckets. Hopping: overlapping fixed buckets. Session: activity-based variable-length windows.

Kafka Streams — window types
KStream<String, PageView> views = builder.stream("page-views");

// Tumbling — count per 1-minute bucket
KTable<Windowed<String>, Long> minuteCounts = views
    .groupByKey()
    .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(1)))
    .count(Materialized.as("views-per-minute"));

// Hopping — 5-min window advancing every 1 min
KTable<Windowed<String>, Long> hoppingCounts = views
    .groupByKey()
    .windowedBy(TimeWindows.ofSizeAndGrace(Duration.ofMinutes(5), Duration.ofSeconds(30))
        .advanceBy(Duration.ofMinutes(1)))
    .count();

// Session — user session with 30-min inactivity gap
KTable<Windowed<String>, Long> sessionCounts = views
    .groupByKey()
    .windowedBy(SessionWindows.ofInactivityGapAndGrace(
        Duration.ofMinutes(30), Duration.ofMinutes(5)))
    .count();

Key Points to Remember

  • 1Tumbling: fixed, non-overlapping — "count per minute"
  • 2Hopping: fixed, overlapping — "5-min rolling window, updated every minute"
  • 3Session: closes after inactivity gap — perfect for user sessions
  • 4Grace period allows late records; after grace, late records are dropped
  • 5Sliding windows update continuously with every event arrival

Interview Questions

Sign in to ask Aria
1

What is the difference between a tumbling and a hopping window?

MediumConfluent
2

When would you use a session window instead of a tumbling window?

MediumAmazon
3

What is a grace period in windowed operations and why is it needed?

HardLinkedIn
4

How does Kafka Streams handle late-arriving records in windowed aggregations?

HardNetflix
5

How do you emit early, speculative window results before the window closes?

HardUber

Ask Aria about Kafka Streams Windowing

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…