Event Streaming LabPython · Bytewax

Chapter 15 · Explicit correction model

Correct a late result

Closing a window and forgetting it are separate decisions.

Prerequisite When can time move forward?.

What you’ll learn

Distinguish watermark delay, allowed lateness, initial emission and state cleanup.

The question

The first sales result for [0,10) is £10. A delayed £5 order for that window arrives after its first emission. You can reject it, revise the result, or store a correction elsewhere. The choice must reach the sink as well as the window operator.

In the previous lab, Bytewax routed records below its watermark to the late output. This Python model illustrates another policy. When the watermark reaches the window end at 10, the model emits its first total. It can still replace that total when an eligible record arrives before cleanup. Cleanup deletes this window’s state when the watermark reaches 10 plus the allowed lateness.

Emission, correction and cleanup are separate boundaries
  1. First emissionTotal 10A provisional answer is visible.
  2. Eligible late recordRevision: total 15The authored policy corrects that answer.
  3. Cleanup boundaryForget retained stateA later arrival cannot use that state.
  4. After cleanupLate-data decisionRoute or reconcile under a new contract.

Conceptual diagram. This is the labelled correction reference policy. It is not presented as a built-in Bytewax retraction operator.

Predict the result

All three orders belong to the window from 0 up to 10 seconds. The model receives a (£10), advances the watermark to 10, receives b (£5), advances the watermark to 15, then receives c (£7).

The watermark reaches 10, then b arrives, then the watermark reaches 15, then c arrives. Which late event can still correct the window?

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.

{
  "updates": [
    {
      "window": 0,
      "revision": 1,
      "total": 10
    },
    {
      "window": 0,
      "revision": 2,
      "total": 15
    }
  ],
  "late": [
    "c"
  ],
  "trace": [
    {
      "action": "event a",
      "watermark": -1,
      "retained": true,
      "accepted_total": 10,
      "revision": 0,
      "late": []
    },
    {
      "action": "watermark 10",
      "watermark": 10,
      "retained": true,
      "accepted_total": 10,
      "revision": 1,
      "late": []
    },
    {
      "action": "event b",
      "watermark": 10,
      "retained": true,
      "accepted_total": 15,
      "revision": 2,
      "late": []
    },
    {
      "action": "watermark 15",
      "watermark": 15,
      "retained": false,
      "accepted_total": 15,
      "revision": 2,
      "late": []
    },
    {
      "action": "event c",
      "watermark": 15,
      "retained": false,
      "accepted_total": 15,
      "revision": 2,
      "late": [
        "c"
      ]
    }
  ]
}

Read the result

Compare the revision and total of the first and last rows in updates. Each revision replaces the prior total for that window. late lists orders this policy rejects after cleanup.

Change the experiment

Replace lateness=5 with lateness=0 in the editor. Select Run again. Then replace lateness=0 with lateness=10 and select Run again. Compare updates and late in all three runs. Which arrivals can still revise the first total?

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.

Cleanup and policy boundaries

At watermark 15 this model discards the state required to revise [0,10) when allowed lateness is five. Equality at the cleanup boundary is rejected by this convention. Boundary tests belong in the contract; do not infer them from a diagram. A framework may use a last representable timestamp inside a half-open window.

What a side output preserves

A late side output should retain event identity, the original timestamp and the policy/position that caused rejection. It enables auditing or a later batch correction. It does not automatically repair the already published aggregate. Define who consumes it and how an eventual correction avoids duplicate effects.

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
WorkO(t) work for t actions, plus the cost of copying trace entries and late output history.
StateA scalar accumulator suffices for the sum; this teaching trace retains additional historical snapshots.
MovementThe Python model reads the watermark actions and events from one fixed sequence.
ProgressThe watermark triggers the initial output and later cleanup. An eligible record between those actions can revise the total.
RecoveryA restart would need the accepted total, current revision, progress and the destination’s rule for applying a revision.

Think it through

If a downstream system sums the two replacement totals 10 and 15, what result will it compute, and why is that wrong?

Show hints
  1. Hint 1

    The same window key appears twice.

  2. Hint 2

    A replacement describes a new state, not a new contribution.

  3. Hint 3

    The sink must understand revisions or consume deltas.

Show explanation

It computes 25, although accepted sales total 15. Use a keyed upsert with a version rule, a retraction followed by an insertion, or correct deltas. Delayed output alone is not a universal solution: choose whether the product requires provisional results, corrections or a final bounded answer.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs.

  • Framework late-data rules for comparison

    The corrections helper is an authored revision policy. Compare it with a framework’s window retention and late-data rules without treating them as the same API.

  • 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 helper uses explicit watermark actions and a single window. It does not demonstrate actual Flink triggers, distributed progress or exactly-once sink commits.

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