Event Streaming LabPython · Bytewax

Chapter 2 · Real Bytewax

Evolve an event schema

A record can parse and still mean the wrong thing.

Prerequisite Follow an event.

What you’ll learn

Validate event meaning, preserve rejected evidence, and distinguish compatible shape from compatible semantics.

The question

A checkout upgrade switches the amount in each order from pounds to pence. The old reader still accepts the JSON, but interpreting pence as pounds makes the reported value 100 times too large.

A schema describes what fields are required and what they mean. The version field tells the reader which rules to apply. In this example, version 1 uses whole pounds and version 2 uses whole pence. The editor’s normalization code converts known versions to pence and reports why it rejects others.

Predict the result

The editor defines four records: a has 12 pounds, b has 850 pence, c uses unknown version 3 and d has a negative amount.

Should a and b both be multiplied by 100? What should happen to c when no version-3 contract exists?

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.

[
  {
    "id": "a",
    "pence": 1200
  },
  {
    "id": "b",
    "pence": 850
  },
  {
    "id": "c",
    "rejected": "unknown version"
  },
  {
    "id": "d",
    "rejected": "invalid amount"
  }
]

Read the result

Find pence for a and b and rejected for c and d. The normalizer returns rejection rows so the remaining records can still be processed.

Change the experiment

Replace {'id':'b', 'version':2, 'amount':850} with {'id':'b', 'version':1, 'amount':850} in the editor. Select Run again. Compare b’s pence. How can the same integer still pass validation but represent a different amount?

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.

Compatibility is directional

An old consumer reading a new producer’s event differs from a new consumer reading retained old events. Test both when a topic outlives deployments. Defaults can bridge an added optional field; they cannot determine an unknown unit or restore a removed identifier. Keep stable event IDs through transformations if downstream deduplication relies on them.

Trace a rejected record

Capture the source partition/offset, schema identifier, error reason and original bytes when available. Separate rejection from temporary processing failure: a timeout might merit retry, while an unsupported version usually needs a deployment or data repair. These distinctions prevent infinite retry loops that block an entire partition.

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(n) normalizations for constant-sized records; validation of arbitrary nested payloads also depends on bytes and structure.
StateNo remembered IDs; O(n) output including rejection records.
MovementThe example reads Python dictionaries prepared in the editor. A wire-format decoder would add work that depends on the encoded bytes.
ProgressEach dictionary can be validated as it arrives. The finished list displays accepted data and rejections in input order.
RecoveryKeep the original record and its schema version if a rejected event may be replayed later.

Think it through

Can a dead-letter topic alone make a schema change safe?

Show hints
  1. Hint 1

    Consider who notices and repairs the rejected events.

  2. Hint 2

    Think about identity and ordering when repaired records are replayed.

  3. Hint 3

    A separate destination does not define a repair procedure.

Show explanation

No. Specify alert ownership, original payload and error provenance, replay identity, permissions and retention. Validate backward/forward compatibility for the actual schema format, and distinguish structural changes from changes to units or business definitions. Reprocessing must not silently duplicate earlier effects.

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.

  • A mapper applies your schema policy

    The callback implements normalization and rejection; Bytewax does not infer the amount units or schema version.

  • Finite test input

    Inspect how the fixed record sequence becomes the input to the graph.

  • 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 editor validates four records with rules specific to this exercise. It has no shared schema catalog or separate service for storing rejected data.

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