Event Streaming LabPython · Bytewax

Chapter 4 · Bytewax and simulated broker

Stream live through Kafka

A producer, a socket, and an engine that keeps listening.

Prerequisite Read a partitioned log.

What you’ll learn

Observe how a running Bytewax engine updates each shop total as orders arrive. Explain what an offset tells you about a record.

The question

The dashboard must update while orders are still arriving. A finite list can teach transformations, but it cannot show a producer sending more work to an already-running consumer. This experiment starts separate producer/controller and Bytewax engine processes inside your browser’s Linux guest.

The controller starts an in-memory Kafka simulator and a Kafka producer. A separate Bytewax process reads new records from partition 0 and keeps a running total for each shop. The controller waits for each result before sending the next order, so you can identify which orders arrived after the engine was already processing.

Predict the result

Orders arrive in this order: North £12, South £8, North £5, North £7 and South £9. All five go to partition 0, at offsets 0 through 4.

Which output totals change when the fourth amount changes from 7 to 70? Will the Kafka offsets change?

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.

{
  "broker": "librdkafka in-memory Kafka simulator",
  "transport": "TCP loopback",
  "records": [
    {
      "key": "North",
      "total": 12,
      "offset": 0
    },
    {
      "key": "South",
      "total": 8,
      "offset": 1
    },
    {
      "key": "North",
      "total": 17,
      "offset": 2
    },
    {
      "key": "North",
      "total": 24,
      "offset": 3
    },
    {
      "key": "South",
      "total": 17,
      "offset": 4
    }
  ],
  "live_arrivals": 4,
  "checkpoint": "not configured",
  "trace": [
    {
      "stage": "KafkaSource → keyed state",
      "key": "North",
      "total": 12,
      "offset": 0
    },
    {
      "stage": "KafkaSource → keyed state",
      "key": "South",
      "total": 8,
      "offset": 1
    },
    {
      "stage": "KafkaSource → keyed state",
      "key": "North",
      "total": 17,
      "offset": 2
    },
    {
      "stage": "KafkaSource → keyed state",
      "key": "North",
      "total": 24,
      "offset": 3
    },
    {
      "stage": "KafkaSource → keyed state",
      "key": "South",
      "total": 17,
      "offset": 4
    }
  ]
}

Read the result

Inspect records for each order’s offset, key, and total. The updates interleave the two shop totals, whereas the offsets count every record in partition 0. live_arrivals counts records sent after an earlier result arrived.

Change the experiment

Replace [12, 8, 5, 7, 9] with [12, 8, 5, 70, 9] in the editor. Select Run again. Compare North’s last total with the offset sequence in records. Which evidence tracks business state, and which tracks source position?

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.

Follow one operation across the boundary

Serialization turns an amount into bytes. The producer sends a Kafka request; the simulator assigns a partition-local offset. KafkaSource polls and decodes a message. key_on/stateful_map route and update one shop’s state, then the observed output returns to the controller. None of these steps makes the sink transactional.

Separate positions from time

An offset is a log position, a watermark expresses event-time progress, and a checkpoint coordinates recoverable state. This experiment has offsets and keyed state but no event-time window or configured checkpoint. The time and recovery chapters add those mechanisms separately.

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
WorkPer record: serialize, produce, assign an offset, fetch/decode, update one key and observe output. This handshake includes protocol latency and is not a throughput test.
StateThe mock retains five records and the engine holds two keyed totals. Unbounded logs and key cardinality would require explicit limits.
MovementReal Kafka protocol crosses Linux loopback between controller-owned mock and a separate Bytewax engine process. No external network relay is used.
ProgressRecord n+1 is produced only after result n arrives. Four arrivals therefore occur after the consumer is demonstrably running.
RecoveryNo checkpoint is configured. The controller stops the engine after five verified outputs; destroying either owner loses its volatile state.

Think it through

After the controller stops, does the existence of offsets 0..4 prove those records will survive closing this page?

Show hints
  1. Hint 1

    Identify which component owns the log storage.

  2. Hint 2

    The simulator stores records in the controller’s memory.

  3. Hint 3

    Offsets identify positions; storage durability is another contract.

Show explanation

The simulator stores its log in the controller’s memory. That log disappears when the process ends, even when Fast reruns keep Linux ready. Offsets identify positions, and this exercise has no persistent broker storage.

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. Confluent’s API guide is a moving reference; this lab uses confluent-kafka 2.6.1 with an in-memory broker simulator.

Experiment scope

Real Kafka producer/consumer sockets and a real Bytewax engine run in separate Linux processes. The server is a one-broker, in-memory librdkafka simulator. The source tails until the controller stops it; no checkpoint, retention or broker failover is configured.

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