Window assignment is a rule over time and identity
Calculate half-open tumbling and sliding membership, merge event-time sessions, and distinguish assignment from triggering, retention, and output updates.
On this page
The question for today
What you’ll understand
- Assign events to half-open fixed windows without boundary duplication.
- Calculate multiple memberships for overlapping sliding windows.
- Explain why late bridging events can merge previously separate sessions.
A window makes a finite question from an unbounded stream, but the word hides several independent choices. Which events belong together? When should an answer be emitted? How long can it change? Start with membership before discussing triggers. A correct boundary calculation prevents an event at exactly ten seconds from being counted in the wrong period, while a clear session rule explains why one late event can revise more than one earlier result.
Half-open intervals avoid double-counting fixed boundaries
A fixed time window includes its start and excludes its end. For zero-aligned tumbling windows of width W, timestamp t belongs to the interval beginning at floor(t/W)W. Timestamp 10 therefore belongs to [10,20), not [0,10), when W=10 seconds. Offsets and time zones can change alignment, so state those choices before calculating. Discrete timestamp implementations use a largest included timestamp below the exclusive end; do not confuse an event's membership with the exact implementation rule that fires a timer or cleans up retained state.
Sliding windows have a width and a slide. If width exceeds slide, one event can contribute to several overlapping windows, increasing state and output work. Membership means a start s on the chosen alignment grid satisfies s≤t<s+W. Count the matching starts rather than dividing timestamp by width and assuming one answer. A downstream consumer also needs window identity if it will combine, replace, or audit outputs. A bare aggregate value does not announce whether it belongs to one window, an updated pane, or a different key.
Sessions can change their shape when history arrives late
A session groups activity separated by less than the configured inactivity gap under its assigner and merge rules. In a simple event-time construction, each event proposes an interval extending from its timestamp through a gap, and overlapping proposed intervals merge. A late event can bridge two earlier groups, so sessions are not fixed bins whose identities can always be finalized on first output. The exact equality-at-gap behavior follows the implementation's interval and merge rules; choose examples with strict overlap when the lesson does not need that boundary detail.
For a session aggregate, a merge must combine the relevant state and coordinate triggers and output corrections. Taking the sum of two already emitted counts and then adding a bridging event can work only if the downstream result protocol retracts or replaces the older results consistently. Otherwise the same activity can appear in old sessions and the new merged session. Flink 2.1 distinguishes window assignment, triggers, and allowed lateness; custom behavior needs compatibility with merging windows. Do not assume that a trigger appropriate for nonmerging tumbling windows automatically has a valid merge contract.
Keep phase repetition separate from full window identity
An event-time trigger can emit an initial answer and later emit a correction while state remains retained. Processing-time triggers can offer earlier provisional answers without proving event-time completeness. Allowed lateness affects how long eligible late events can modify a window, while cleanup determines retained resources. Describe the output mode—replacement, retraction, or accumulated delta—so a sink does not count every firing as a new independent business result. Test boundary timestamps and bridging arrivals in different orders, and inspect final keyed results rather than only the number of output messages.
The modular lab illustrates why repeating phase is not full time identity. Reset, explicitly set BOTH m=5 and a=1, predict residues zero through four, then five Steps: n=floor(2t) reaches five and the residue returns to zero. That does not make event-time windows [0,5) and [5,10) the same window. Edits retain n through elapsed time; Reset is needed for a fresh orbit. This finite additive cycle has no timestamps, window assignment, sessions, triggers, or lateness. Use it only to separate phase within a cycle from the unbounded count of completed cycles.
Work it through
One event can belong to three sliding windows
Use zero-aligned event-time windows with width ten seconds and slide four seconds. Find all memberships for t=9 seconds. Separately, with a five-second session gap, consider events at 0, 8, and a late event at 4 seconds.
Sliding starts must be multiples of four with s≤9<s+10. The valid starts are 0,4,8, producing [0,10), [4,14), and [8,18). Start −4 ends at 6 and does not qualify.
The first two session proposals are [0,5) and [8,13), initially separate. The late event proposes [4,9), overlapping both strictly.
The three session proposals merge to [0,13) in this illustrative gap construction. Any previously emitted separate-session results need the declared correction protocol rather than being blindly added as new independent outputs.
Sliding overlap creates three memberships here; session merging creates one revised group. Neither result determines when an output is final.
The living notebook
Explore the model
Interactive lab · modular
Repeated phase does not repeat the whole window
Reset, set BOTH m=5 and a=1, predict, then Step five times. n=floor(2t) reaches five and residue returns to zero; edits retain t. This finite additive orbit illustrates repeated phase only, not real timestamps, tumbling/sliding assignment, sessions, triggers, or a watermark.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA cyclic phase must be paired with a cycle count or actual time identity; equal residues do not identify equal event-time windows.
Pause & reason
Check your understanding
Your turn
Try it yourself
For zero-aligned width eight seconds and slide four seconds, list the windows containing t=8. Explain why [0,8) is not one of them.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Enumerate aligned starts satisfying s≤8<s+8, retaining the strict upper comparison.
Compare with the solution
The memberships are [4,12) and [8,16). Window [0,8) excludes its end, so timestamp eight is not a member. This two-membership result does not imply two distinct business events; it reflects overlapping aggregation questions.
Keep the idea
Take it with you
- Half-open window membership separates adjacent fixed intervals cleanly.
- Sliding overlap and session merging change state and output requirements.
- Assignment, triggering, allowed lateness, and cleanup are independent choices.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.