Chapter 27 · Bytewax + identity policy
Replay without double counting
One order can have two deliveries.
Prerequisite Recognize sequences and absence.
Separate event identity from delivery and explain the scope of deduplication.
The question
A consumer restarts and reads an order again. Replaying is useful for rebuilding state, but naïvely adding every delivery inflates revenue.
Replaying a log can deliver the same logical event again. key_on uses the event ID; our remember callback stores True after the first delivery and emits None on duplicates. A filter removes those duplicate markers. fold_final sums the retained orders and emits only at end of input. The first-delivery rule is authored policy implemented through real Bytewax keyed state.
- DeliveriesA repeated identityThe source can replay an already seen event.
- Identity stateSeen or new?The retention horizon matters.
- Accepted eventsUpdate the aggregateSuppress the duplicate under this policy.
Conceptual diagram. This executes keyed identity state locally. It does not coordinate an arbitrary external effect atomically.
Predict the result
The source sends the full order list followed by a second delivery of a and b. The initial key is the order ID.
We replay a and b after the five original events. What are the totals before and after deduplication?
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.
[ 41 ]
Read the result
Read the final total in the console. The source delivers seven records and the identity key lets five distinct orders contribute to the sum. Bytewax emits the final sum when its input ends.
Change the experiment
Replace lambda order: order['id'] with lambda order: order['shop'] in the editor. Select Run again. Compare the new total with £41. The first North and South orders contribute £12 and £8. Which distinct orders now share a key and are incorrectly suppressed?
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.
Inspect the selected implementation
The visible remember callback stores one boolean for each event ID through Bytewax keyed state. A linear list of remembered IDs would have a different lookup cost. The operation-effort chapter instruments that separate reference, rather than attributing its comparisons to the engine.
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 | One keyed state callback per delivery, with a constant-sized True state value. Routing/key lookup is engine work, distinct from an O(k) linear-list reference algorithm. |
| State | One retained state entry per distinct logical ID, plus the scalar final total. Unbounded identity cardinality still means unbounded state. |
| Movement | Bytewax routes identical IDs to the same logical state. This single-worker experiment does not measure a distributed shuffle. |
| Progress | The authored remember callback rejects repeats. fold_final emits the total only at EOF, unlike an incremental running total. |
| Recovery | Each run starts without remembered IDs. In a deployed pipeline, replay protection depends on which identities the producer, operator and destination can recover. |
Think it through
Would remembering every ID forever be a safe design for an unbounded event stream?
Show hints
Hint 1
Consider both memory and identity lifetime.
Hint 2
A bounded retention period changes the guarantee.
Hint 3
A duplicate might arrive after an ID has expired.
Show explanation
Not without a bounded identity space or managed storage. Time-limited deduplication bounds memory but accepts duplicates arriving after expiry. Define ID uniqueness and retention together, and make those limits part of the contract.
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.
Deduplicate, then accumulate
op.key_on(new tab)op.stateful_map(new tab)op.filter(new tab)op.map(new tab)op.fold_final(new tab)Inspect identity selection, retained seen-state and final accumulation as separate operations.
Graph construction and input interfaces
Read these signatures for the graph and finite test input constructed in the editor. The course collection helper attaches an output and starts execution.
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
The identity policy retains one state entry per distinct ID with no expiry. This is neither a Kafka idempotent-producer guarantee nor end-to-end exactly-once output.
Primary reference →