Event Streaming LabPython · Bytewax

Chapter 11 · Bytewax event-time windows

Close a window

Give an endless calculation a boundary.

Prerequisite Choose the right clock.

What you’ll learn

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.

Event time determines window membership
Timestamped events0, 4, 10, 14Assign each event under the declared interval convention.
  1. Window [0,10)a and bThe upper boundary is excluded.
  2. 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.

Ctrl+Enter or ⌘+Enter runs the code.

Edits are saved in this browser when storage is available.

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?

Return to the editor
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.

QuestionMechanism and boundary
WorkWith r overlapping windows, each accepted event can contribute to r collectors. Emitting and sorting the collected events adds work that depends on their count.
Statecollect_window retains event payloads, not only a scalar sum. Retained bytes depend on input rate, window width, overlap and event size.
MovementA real local Bytewax event-time window; no broker transport, replication or distributed exchange is measured here.
ProgressEventClock and window close rules govern live progress; bounded-source EOF flushes unfinished windows in this fixture.
RecoveryA 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
  1. Hint 1

    Find the clock supplied to the window operator.

  2. Hint 2

    EventClock explicitly extracts each record’s timestamp.

  3. 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.

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 →
Reading marks track what you’ve read independently of your runs.