Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Time and Windows

Session Windows, Global Windows and Triggers

Group events by activity with Flink session windows and gap detection, build custom windows with GlobalWindows, and control firing with triggers, evictors and window joins.

Tumbling and sliding windows slice time into fixed pieces. But some real groupings are shaped by the data, not the clock: a user's visit lasts as long as they keep clicking, and ends after a pause. Flink calls these session windows. For anything else, there are global windows with custom triggers, which let you decide when a window produces output.

This lesson follows Tumbling and Sliding Windows.

Session windows

A session window groups events of one key that are close together. It closes when no event arrives for a gap duration. Session windows have no fixed size or start; each key has its own sessions.

Java
clicks
    .keyBy(Click::userId)
    .window(EventTimeSessionWindows.withGap(Duration.ofMinutes(30)))
    .aggregate(new CountClicks());

How it works internally: every event creates a tiny window [ts, ts + gap). Overlapping windows of the same key are merged. When a late event bridges two earlier sessions, they merge into one. Windows are only complete (and fire) when the watermark passes the end of the merged window.

For gaps that depend on the event (premium users get a longer gap), use a dynamic gap:

Java
.window(EventTimeSessionWindows.withDynamicGap(click -> click.isPremium() ? 3_600_000L : 1_800_000L))

Because sessions merge, session windows need an aggregation that can combine two accumulators: AggregateFunction.merge(...) must be implemented correctly, and reduce works naturally.

Stand-in: session windows by gap

Java Stand-in: events merged into sessions with a 30-minute gap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Ana has two sessions (a 65-minute pause splits them), and Bo has two single-click sessions. Each session's end is the last event plus the gap, which is the time at which Flink would fire it.

Global windows

GlobalWindows.create() puts all events of a key into one window that never ends. On its own it never fires, so you must supply a trigger. This is the building block for "every N events" windows and other custom logic.

Java
orders
    .keyBy(Order::customerId)
    .window(GlobalWindows.create())
    .trigger(PurgingTrigger.of(CountTrigger.of(100)))     // fire and clear every 100 orders
    .aggregate(new Total());

In older versions there was a countWindow(100) shortcut doing exactly this. PurgingTrigger wraps another trigger and clears the window's contents after each firing.

Triggers: deciding when to fire

Every window assigner comes with a default trigger (event-time windows fire when the watermark passes the end). You can replace it with .trigger(...). A trigger reacts to three events and returns a TriggerResult:

  • onElement: every record added to the window.
  • onEventTime: an event-time timer fires.
  • onProcessingTime: a processing-time timer fires.

TriggerResult options: CONTINUE (do nothing), FIRE (compute and emit, keep the contents), PURGE (clear without emitting), FIRE_AND_PURGE.

Built-in triggers: EventTimeTrigger, ProcessingTimeTrigger, CountTrigger.of(n), ContinuousEventTimeTrigger.of(interval), ContinuousProcessingTimeTrigger.of(interval) (early results inside a window), PurgingTrigger, ProcessingTimeoutTrigger.

A popular pattern is early firing: a daily window that also emits partial results every minute:

Java
.window(TumblingEventTimeWindows.of(Duration.ofDays(1)))
.trigger(ContinuousProcessingTimeTrigger.of(Duration.ofMinutes(1)))

With FIRE, the contents stay, so each early result is a refined version of the same window, and consumers must treat them as updates of one value (an upsert sink).

Stand-in: a count trigger on a global window

Java Stand-in: fire every 3 elements and purge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Each key reaches three elements on its own schedule and fires independently. Key b has fired once with [1, 2, 3] and keeps [4].

Evictors

An evictor removes elements from the window before and/or after the window function runs (evictBefore, evictAfter), for example CountEvictor.of(100) keeps only the last 100 elements. Evictors force Flink to buffer all elements, so incremental aggregation is no longer possible: use them sparingly.

Window joins and cogroups

Two keyed streams can be joined over a window: elements with the same key that fall into the same window are paired.

Java
orders.join(payments)
      .where(Order::orderId)
      .equalTo(Payment::orderId)
      .window(TumblingEventTimeWindows.of(Duration.ofMinutes(5)))
      .apply((order, payment) -> new Paid(order, payment));

An inner window join emits the cross product per key and window. Pairs split across a window boundary are missed. When that is a problem, use an interval join, which matches each element of one stream with elements of the other within a time range: orders.keyBy(...).intervalJoin(payments.keyBy(...)).between(Duration.ofMinutes(-1), Duration.ofMinutes(10)).process(...). coGroup is the most general form, handing you both sides' elements as two iterables.

Choosing a window type

Need Window
Fixed buckets (per minute, per hour) Tumbling
Moving average, trailing N minutes Sliding
Group by user activity Session
Every N events, custom conditions Global + trigger
Pair elements from two streams by time Window join or interval join

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does a session window for a key close?

    Sessions are defined by activity; a pause longer than the gap ends them.
  2. 2. Why must aggregation in a session window be mergeable?

    Merged windows need their accumulators combined, so merge() must be correct.
  3. 3. What does a GlobalWindows assigner need to produce output?

    Without a trigger the global window never fires.
  4. 4. What does TriggerResult.FIRE_AND_PURGE do?

    FIRE keeps the contents; FIRE_AND_PURGE emits and removes them.

Summary

  • Session windows follow activity: they close after a gap with no events and merge when late events bridge them.
  • Global windows never end, so a trigger decides when to fire; count triggers give "every N events" behaviour.
  • Triggers return CONTINUE, FIRE, PURGE or FIRE_AND_PURGE; early firing gives partial results inside a long window.
  • Evictors prevent incremental aggregation; window joins pair elements per window and interval joins pair them within a time range.

Next: Late Data and Allowed Lateness.