Event Streaming LabPython · Bytewax

Chapter 12 · Bytewax session windows

Merge a session

A late event can connect two groups.

Prerequisite Close a window.

What you’ll learn

Explain why session boundaries depend on data and why late bridging can require merging state.

The question

A shopper clicks at times 0 and 8. With a five-second inactivity gap, these initially look like two sessions. A delayed click at 4 arrives and connects them. Counting sessions without revising earlier assumptions gives the wrong answer.

A session groups nearby events for a key according to an inactivity gap, rather than fixed boundaries. This helper uses real Bytewax SessionWindower with gap=5 and collect_window to retain members. Its EventClock waits 30 seconds with frozen wall time, keeping this fixture open long enough for the out-of-order bridge at time 4. The trace lists final emitted sessions; intermediate engine state is not exposed.

A bridging event can connect earlier observations
  1. Event-time observations0, 8, then 4; later 20Arrival order differs from timestamp order.
  2. Session gap 54 bridges 0 and 8Both neighboring gaps are within the session rule.
  3. Resulting groups[0, 4, 8] and [20]The distant observation stays in its own session.

Conceptual diagram. This shows the starter event-time relationships. Sessions are not fixed-width buckets.

Predict the result

Four event times arrive as 0, 8, 4 and 20 seconds, all with the same key. The clock uses a 30-second wait to keep this fixture eligible.

After arrivals 0 and 8 there are two groups. After 4 arrives, how many groups should those three events occupy?

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.

{
  "sessions": [
    {
      "first": 0,
      "last": 8,
      "events": [
        0,
        4,
        8
      ]
    },
    {
      "first": 20,
      "last": 20,
      "events": [
        20
      ]
    }
  ],
  "gap": 5,
  "trace": [
    {
      "emission": "closed session",
      "first": 0,
      "last": 8,
      "events": [
        0,
        4,
        8
      ]
    },
    {
      "emission": "closed session",
      "first": 20,
      "last": 20,
      "events": [
        20
      ]
    }
  ]
}

Read the result

Read the events list of each row in sessions. The first session holds 0, 4 and 8 even though 8 arrived before 4. This result shows final sessions emitted by Bytewax.

Change the experiment

Replace gap=5 with gap=3 in the editor. Select Run again. Then replace gap=3 with gap=8 and select Run again. Compare session membership across your three runs. Use the arrival order 0, 8, 4, 20 to explain how the delayed event at 4 changes the final grouping.

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.

The merge function is part of correctness

A session aggregate needs a way to combine state when groups merge. Sum and count have simple associative combinations. A median or ordered event history may require more information. Here Bytewax collect_window retains full members; a compact accumulator would have a different memory contract.

A session is a policy, not a psychological fact

A five-second gap is an analytical definition. It does not prove whether two visits belong to one person or one intent. State what key identifies a visitor, how identity changes are treated, and how late events affect already published session counts.

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
WorkThe real SessionWindower groups by event-time gaps. Cost depends on open sessions, merging and collected event ordering; do not infer an indexed complexity bound from this fixture.
Statecollect_window retains each accepted event until its session emits. A long bridged session can retain unbounded history without a separate limit.
MovementOne Bytewax worker groups events for one key. A distributed version must route events for a given key to the same owner.
ProgressThe clock leaves these sessions eligible for merging. Bytewax emits the remaining sessions when input ends.
RecoveryRestoring open sessions would require the source position, retained events and clock progress together.

Think it through

If the first session was already emitted and its state discarded, can the late bridging event be handled by simply adding one to a session counter?

Show hints
  1. Hint 1

    The bridge may change the number of sessions as well as their aggregate.

  2. Hint 2

    State is needed to know which sessions merge.

  3. Hint 3

    Previously emitted results may need retraction or replacement.

Show explanation

No. An exact correction may require the earlier session state and a way to revise its output. Choose a lateness/retention policy and a changelog contract, or route the bridge to a separate correction process once state is gone. A fixed-size counter cannot reconstruct forgotten membership.

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 SessionWindower with one key, a fixed wall clock and a 30-second wait. Bytewax emits any open sessions when the source ends. Processing-time triggers and progress across multiple inputs require separate examples.

Primary reference →
Reading marks track what you’ve read independently of your runs.