Stackbook Logo
messagingestablished · high operational burden

Stream Windowing

Also known as: windowed aggregation, tumbling and sliding windows

Intent

Group an unbounded event stream into finite windows so aggregations can be computed and emitted.

Problem

Aggregates such as counts per minute need a boundary, but streams never end and events arrive late or out of order. Using arrival time gives wrong answers when delays occur.

Forces

  • •Streams are unbounded
  • •Event time and processing time differ
  • •Late events must be handled
  • •State per open window consumes memory

Solution

Overview

Windowing slices time into pieces that can be aggregated. The key discipline is using event time, the moment something happened, not the time it reached the processor.

A watermark is the engine belief that no more events older than a given time will arrive.

Pick the window type to match the question. Tumbling windows fit per-minute counts, sliding windows fit moving averages that update more often than the window length, and session windows fit user activity separated by quiet gaps. Each assigns events differently and carries different state costs, since sliding windows hold each event in several windows.

How it works

Each event is mapped to one or more windows by its timestamp. When the watermark passes a window end plus allowed lateness, the aggregate is emitted and state released.

window = floor(event.ts / 60s) * 60s     # tumbling, 1 min
counts[(key, window)] += 1

when watermark >= window + 60s + lateness:
    emit(key, window, counts.pop((key, window)))

Handling lateness is a product decision as much as a technical one. A longer allowed lateness improves accuracy but delays final results and holds more state. Many systems emit an early provisional result and then updates as late data arrives. Whatever you choose, count dropped late events as a metric so you know the real cost of your watermark setting.

Pitfalls

  • Aggregating by processing time, so retries skew results.
  • A single idle partition holding back the watermark for the whole job.
  • Setting lateness too tight and discarding real data without measuring it.
  • Choosing session gaps that never close for bots.

Key takeaways

  • Use event time with watermarks.
  • Pick the window type to fit the question asked.
  • Route late data to a side output and measure it.
  • Watch state size per open window.

✓ When to Use

  • •Metrics, rate limits or fraud signals over time ranges
  • •Sessionization of user activity
  • •Joining streams within a time bound

✗ When Not to Use

  • •Results need exact all-time totals
  • •Event timestamps are unreliable
  • •A batch job over stored data is sufficient

Pros

  • +Bounded state and timely results
  • +Event-time correctness with watermarks
  • +Flexible window shapes

Cons

  • −Late data forces tradeoffs
  • −Watermark tuning is subtle
  • −State and checkpoints add operational weight

Cost Profile

Infrastructure

High - stream processing cluster

Operational

High - state, checkpoints, lag

Cognitive

High - time semantics

Failure Modes

  • Watermark stalls when one partition goes idle

  • Late events silently dropped

  • Window state grows unbounded with long sessions

Real-World Examples

Alternatives

    Related Patterns

    • consumer-groups-partitioning
    • message-ordering

    Competency Domains

    data statedistribution communication