Chapter 3 · Kafka protocol simulator
Read a partitioned log
Ordering has a scope.
Prerequisite Evolve an event schema.
Distinguish a topic, a partition and an offset, and explain ordering within a partition.
The question
The Python pipeline explains how to process records. Kafka answers a different question: how can producers append events that independent consumers can read later? Start by making that log visible.
A Kafka topic is a named log split into partitions. Each partition orders its own records, and an offset is a record’s position in that partition. The editor starts librdkafka’s in-memory Kafka simulator, writes orders with a Kafka producer and reads them with Bytewax. The example explicitly sends North orders to partition 0 and South orders to partition 1.
- Partition 00:a · 1:c · 2:dAn ordered North history.
- Partition 10:b · 1:eAn ordered South history.
Conceptual diagram. The partitions are parallel histories; no single offset orders both. Real clients read the Kafka simulator.
Predict the result
A Kafka producer explicitly routes North orders to partition 0 and South orders to partition 1. Their respective event times are a=0, c=8, d=3 and b=12, e=25 seconds.
Will order b and order c have the same offset even though they are different events?
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.
{
"0": [
{
"offset": 0,
"id": "a"
},
{
"offset": 1,
"id": "c"
},
{
"offset": 2,
"id": "d"
}
],
"1": [
{
"offset": 0,
"id": "b"
},
{
"offset": 1,
"id": "e"
}
]
}Read the result
Compare the id and offset rows in partitions 0 and 1. Partition 0 contains a, c and d. Partition 1 starts its own offsets at 0 and contains b and e.
Change the experiment
Replace result = partition_log() with result = partition_log()["0"] in the editor. Select Run again. Read the IDs in increasing offset order. Their event times are a=0, c=8 and d=3 seconds. Where do append order and event-time order differ?
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.
Partition count and keyed order
Two partitions permit at most two active partition assignments within a traditional consumer group, although downstream computation may use other parallelism. More partitions do not create a total order. A routing change can move a key’s future records while older records remain in another 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.
| Question | Mechanism and boundary |
|---|---|
| Work | Each produced record is serialized and sent through Kafka protocol, then decoded by KafkaSource. Work includes bytes, polling and metadata requests. |
| State | The mock retains the log in memory; Bytewax and this page collect bounded results. This is not a disk/replica storage estimate. |
| Movement | Actual Linux loopback TCP connects clients to an in-memory Kafka simulator. Explicit shop routing chooses partition 0 or 1. |
| Progress | Offsets are ordered only within each partition. tail=False ends the finite read at partition EOF; output is sorted for display. |
| Recovery | Offsets enable seeking while the owning mock remains alive. Closing that process discards the log, so broker crash durability is not established. |
Think it through
Can you merge two partitions by offset and claim that this reproduces a single global arrival order?
Show hints
Hint 1
Two records can share an offset.
Hint 2
Each partition assigns its own positions.
Hint 3
You need another ordering rule to compare different partitions.
Show explanation
No. Offset order is local to a partition. Merging equal offsets invents an order the logs did not establish. If related events require ordered processing, choose a key and partitioning contract that keeps them together; changing partition counts and producer behavior can affect that 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. Confluent’s API guide is a moving reference; this lab uses confluent-kafka 2.6.1 with an in-memory broker simulator.
The Bytewax Kafka source
Read how topics and partitions become input. The lab uses this connector against librdkafka’s simulator, not an Apache Kafka deployment.
Send records and inspect offsets
Inspect explicit partition selection and the delivery callback.
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 producer, Kafka protocol traffic and Bytewax connector are real. The broker is an in-memory librdkafka simulator. Reading this fixture does not establish disk durability, replication, retention or cross-partition ordering.
Primary reference →