Event Streaming LabPython · Bytewax

Chapter 6 · Real Kafka client / simulator

Give consumers their own position

Reading does not erase the log.

Prerequisite Retain or compact a log.

What you’ll learn

Explain independent consumer-group positions and identify what an offset commit records.

The question

Sales needs current totals. Audit needs every retained event. Both should read the same topic without consuming records away from each other.

A consumer group stores its own next position per partition. This helper sends actual OffsetCommit requests to the simulator, closes that client, then opens a new client and fetches the stored positions. Sales starts at the editable next position and audit starts at zero. Both assign partitions explicitly; this tests offset storage and independent reads, not a membership rebalance.

Readers do not have to share progress
Retained logPartitioned recordsOne history can serve independent readers.
  1. Sales readerOwn next positionAdvance under the sales processing contract.
  2. Audit readerOwn next positionAdvance independently under the audit contract.

Conceptual diagram. A committed position does not preserve source data beyond retention. Inspect the actual per-partition positions in the result.

Predict the result

Both groups read the same five orders. The edited setting changes the sales group’s next offset in both partitions. Audit always starts at 0.

Sales has committed position 1 in both partitions. Audit starts at 0. Will audit still see the records sales has already processed?

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.

{
  "sales": {
    "0": [
      {
        "offset": 1,
        "id": "c"
      },
      {
        "offset": 2,
        "id": "d"
      }
    ],
    "1": [
      {
        "offset": 1,
        "id": "e"
      }
    ]
  },
  "audit": {
    "0": [
      {
        "offset": 0,
        "id": "a"
      },
      {
        "offset": 1,
        "id": "c"
      },
      {
        "offset": 2,
        "id": "d"
      }
    ],
    "1": [
      {
        "offset": 0,
        "id": "b"
      },
      {
        "offset": 1,
        "id": "e"
      }
    ]
  },
  "note": "Real group-offset commit/fetch requests to an in-memory Kafka simulator; next position, not a durable broker guarantee."
}

Read the result

Compare the IDs in sales and audit by partition. Sales reads from its committed next offset, while audit reads from offset 0. Reading changes a consumer’s position, and retention determines what remains in the log.

Change the experiment

Replace committed=1 with committed=0 in the editor. Select Run again. Compare the two groups in sales and audit. Then replace committed=0 with committed=2 and select Run again. Which IDs can sales still read, and does audit change?

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.

Assignments can move

During a rebalance an assignment moves between group members. A consumer must not continue committing or applying uncontrolled effects as if it still owned the partition. Real client/framework contracts manage assignment epochs and in-flight work. The exercise assigns partitions explicitly and does not trigger a rebalance.

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
WorkTwo groups send actual commit/fetch requests and read their selected records. Protocol round trips and fetched bytes add to record handling work.
StateThe simulator stores one next position per group/partition. Returned lists retain each group’s observed records for inspection.
MovementReal Kafka Consumer clients talk over loopback to a simulator. Explicit assignments avoid claiming a JoinGroup or rebalance experiment.
ProgressA committed next position selects restart input. It does not prove corresponding business effects are committed safely.
RecoveryOffsetCommit storage is in-memory here. Bytewax KafkaSource instead uses its own recovery positions; neither is an external-sink transaction by itself.

Think it through

If a consumer commits position 2 before its sink has written the record at offset 1, what happens when it crashes?

Show hints
  1. Hint 1

    Restart begins at the committed next position.

  2. Hint 2

    The log may still hold the skipped record.

  3. Hint 3

    Retention and progress are different contracts.

Show explanation

It resumes at 2 and skips unfinished work at 1 unless another mechanism detects it. Commit-after-write can instead repeat an effect after a crash. The process-recovery drill deliberately waits for a completed checkpoint and does not test that external-write race. Coordinating source progress with sink effects needs an explicit contract; changing a position alone does not solve it.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs. 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 consumer commit/fetch requests run against an in-memory simulator. The test uses explicit assignments, without JoinGroup rebalancing or broker persistence. Bytewax KafkaSource separately manages positions through Bytewax recovery, not Kafka group commits.

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