Windowing and Stateful Aggregations in Kafka Streams
Learn to perform time-windowed aggregations and manage state stores in Kafka Streams to compute running counts, sums, and metrics over event streams.
Windowing and Stateful Aggregations in Kafka Streams is a free Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Stateful vs Stateless
Operations like map and filter are stateless. Aggregations such as counting or summing require state that persists across records.
Kafka Streams manages this state for you in local state stores.
Grouping a Stream
Before aggregating you group records by key with groupByKey or groupBy.
KGroupedStream<String, Order> grouped =
orders.groupBy((key, order) -> order.getCustomerId());Counting per Key
A simple aggregation: count orders per customer. The result is a KTable backed by a state store.
KTable<String, Long> counts = grouped.count();General Aggregation
Use aggregate for custom accumulation. You provide an initializer and an adder function.
KTable<String, Double> totals = grouped.aggregate(
() -> 0.0,
(key, order, sum) -> sum + order.getAmount());Introducing Windows
Often you want aggregations over a time window, e.g. orders in the last 5 minutes. Kafka Streams supports tumbling, hopping, and session windows.
Tumbling Windows
A tumbling window is fixed-size and non-overlapping. Each record belongs to exactly one window.
grouped
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
.count();Hopping Windows
Hopping windows have a size and a smaller advance, so they overlap. A record can fall into multiple windows.
TimeWindows.ofSizeAndGrace(Duration.ofMinutes(5), Duration.ofSeconds(30))
.advanceBy(Duration.ofMinutes(1));Session Windows
Session windows group records separated by gaps of inactivity — ideal for user sessions where activity bursts then pauses.
grouped
.windowedBy(SessionWindows.ofInactivityGapWithNoGrace(Duration.ofMinutes(10)))
.count();State Store Fault Tolerance
State stores are backed by compacted changelog topics in Kafka. If an instance fails, its state is rebuilt from the changelog on another instance.
Querying State Interactively
Interactive Queries let you read state store values directly from the application, exposing aggregations via a REST endpoint without an external database.
ReadOnlyKeyValueStore<String, Long> store =
streams.store(StoreQueryParameters.fromNameAndType(
"counts", QueryableStoreTypes.keyValueStore()));Putting It Together
Windowed, stateful aggregations turn raw event streams into live metrics. Group by key, choose a window type, aggregate, and optionally expose the state via interactive queries.
Quick Check
Test your understanding of windowing.
Recap
You learned windowing and stateful aggregations.
- Group with
groupByKeybefore aggregating. - Use
countoraggregateto build a KTable. - Tumbling, hopping, and session windows handle time differently.
- State stores are fault-tolerant via changelog topics.
Frequently asked questions
Is the “Windowing and Stateful Aggregations in Kafka Streams” lesson free?
Yes — the full text of “Windowing and Stateful Aggregations in Kafka Streams” is free to read here on the web, and the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course, upgrade to CoddyKit PRO.
What will I learn in “Windowing and Stateful Aggregations in Kafka Streams”?
Learn to perform time-windowed aggregations and manage state stores in Kafka Streams to compute running counts, sums, and metrics over event streams. You practise Advanced Spring Boot 4: Event-Driven Architecture (Kafka) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
No prior experience is required. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Windowing and Stateful Aggregations in Kafka Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson?
Yes. Every Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Kafka Streams
- Stream Processing with KStream & KTable
- Building a Simple Stream Application
- Windowing and Stateful Aggregations in Kafka Streams