Chapter 15 · Explicit correction model
Correct a late result
Closing a window and forgetting it are separate decisions.
Prerequisite When can time move forward?.
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.
- First emissionTotal 10A provisional answer is visible.
- Eligible late recordRevision: total 15The authored policy corrects that answer.
- Cleanup boundaryForget retained stateA later arrival cannot use that state.
- 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.
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.
{
"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?
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.
| Question | Mechanism and boundary |
|---|---|
| Work | O(t) work for t actions, plus the cost of copying trace entries and late output history. |
| State | A scalar accumulator suffices for the sum; this teaching trace retains additional historical snapshots. |
| Movement | The Python model reads the watermark actions and events from one fixed sequence. |
| Progress | The watermark triggers the initial output and later cleanup. An eligible record between those actions can revise the total. |
| Recovery | A 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
Hint 1
The same window key appears twice.
Hint 2
A replacement describes a new state, not a new contribution.
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 →