Event Streaming LabPython · Bytewax

Chapter 27 · Bytewax + identity policy

Replay without double counting

One order can have two deliveries.

Prerequisite Recognize sequences and absence.

What you’ll learn

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.

Stable identity separates a logical event from its deliveries
  1. DeliveriesA repeated identityThe source can replay an already seen event.
  2. Identity stateSeen or new?The retention horizon matters.
  3. 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.

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.

[
  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?

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.

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.

QuestionMechanism and boundary
WorkOne 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.
StateOne retained state entry per distinct logical ID, plus the scalar final total. Unbounded identity cardinality still means unbounded state.
MovementBytewax routes identical IDs to the same logical state. This single-worker experiment does not measure a distributed shuffle.
ProgressThe authored remember callback rejects repeats. fold_final emits the total only at EOF, unlike an incremental running total.
RecoveryEach 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
  1. Hint 1

    Consider both memory and identity lifetime.

  2. Hint 2

    A bounded retention period changes the guarantee.

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

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