Chapter 11 · Bytewax event-time windows
Close a window
Give an endless calculation a boundary.
Prerequisite Choose the right clock.
Assign records to event-time windows and distinguish tumbling from sliding membership.
The question
A running total never finishes. The operations team instead asks for ten-second groups of event timestamps. A window gives that calculation a boundary; when its answer becomes visible depends on progress and emission policy.
A tumbling window has a fixed width and no overlap. A sliding window starts more often than its width, so a record can enter several windows. This helper uses real Bytewax SlidingWindower with an EventClock reading each record’s timestamp. A fixed 30-second wait and frozen wall clock keep this finite fixture deterministic. Source completion flushes open windows; these results do not measure how long a live job waits.
- Window [0,10)a and bThe upper boundary is excluded.
- Window [10,20)c and dTimestamp 10 belongs here.
Conceptual diagram. The real window operator uses the stated clock. Bounded source completion also participates in emission.
Predict the result
Four records: a at time 1, b at 7, c at 12 and d at 18 seconds. Frozen wall time and a 30-second wait keep them eligible. The source ends after d.
Events have times 1, 7, 12 and 18. Which values belong in [0,10)? What changes if ten-second windows start every five seconds?
Run the experiment
The editor contains the published example. Select Run with the code unchanged.
Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
A saved draft is open. Select Restore code to see the published example. Recover draft brings back your draft.
Ctrl+Enter or ⌘+Enter runs the code.
Edits are saved in this browser when storage is available.
Technical error details
Your result
Console output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
View the published result
The course build ran the published code to generate this output.
[
{
"start": 0,
"end": 10,
"events": [
"a",
"b"
]
},
{
"start": 10,
"end": 20,
"events": [
"c",
"d"
]
}
]Read the result
Read start, end, and events in each emitted window. The first window includes event times from 0 up to 10 and holds a and b. The second starts at 10 and holds c and d. The result records membership at the end of the source.
Change the experiment
Replace slide=10 with slide=5 in the editor. Select Run again. Keep width=10. Check how often each ID appears in events and find the row whose start is below zero. Why does a window start before the first event?
Runtime, source code and downloads
Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.
Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.
Make overlap visible
For width ten and slide five, an ordinary interior event participates in two windows. Source completion flushes partial buffers, so end-of-input output differs from a never-ending live run. A sum can retain one accumulator per key/window; a collect operator retains the contributing values. Choose the aggregate before estimating memory.
Work, state and recovery costs
Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.
| Question | Mechanism and boundary |
|---|---|
| Work | With r overlapping windows, each accepted event can contribute to r collectors. Emitting and sorting the collected events adds work that depends on their count. |
| State | collect_window retains event payloads, not only a scalar sum. Retained bytes depend on input rate, window width, overlap and event size. |
| Movement | A real local Bytewax event-time window; no broker transport, replication or distributed exchange is measured here. |
| Progress | EventClock and window close rules govern live progress; bounded-source EOF flushes unfinished windows in this fixture. |
| Recovery | A checkpointed live window needs consistent source position, window contents and progress state. This fixture intentionally does not configure recovery. |
Think it through
If a record carries an old timestamp, will this built-in operator automatically move it into an earlier buffer?
Show hints
Hint 1
Find the clock supplied to the window operator.
Hint 2
EventClock explicitly extracts each record’s timestamp.
Hint 3
An earlier event-time window can still accept a record while it remains open.
Show explanation
The clock configuration determines which time is read. Here EventClock explicitly extracts the timestamp, so an accepted out-of-order event can enter its earlier event-time window while that window remains open. A processing-time clock would answer a different question.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs. Bytewax links target the official 0.21.1 definitions and API docstrings.
Window membership and closing
Read window length, offset and the clock’s progress contract. The helper uses SlidingWindower even when slide equals width.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
This run uses a real Bytewax time window with a fixed wall clock, a 30-second wait and four inputs. The source ends after the fourth record, so Bytewax emits any windows still open. Use the course’s native performance guide to measure throughput.
Primary reference →