Partitions and Keys: Where Order Is Preserved
18:20. Twenty, eight, and twenty
November 2, 2184. The reading room is consuming the feed with a group of three consumers — one per partition. Forty minutes ago it ran into ; now it's reporting a different problem: the first consumer can't keep up, while the second is almost idle. The counters on the racks show why: partition 0 gets 20 frames per second, partition 1 gets 8, and partition 2 gets 20.
The reading room has two proposals. First: “Remove the key and let Kafka spread the frames evenly, 16 per partition.” Second: “Let's add a fourth consumer to the group so it can help.”
Without a key, the load really can be spread evenly. But the reading room stitches each antenna's frames into one continuous recording, so the frames from each antenna must arrive in strict order. You'll check for yourself what work the fourth consumer gets.
QUERY: Evenly balanced means the consumer stops complaining. Correct means the recording does.

reading_room group read and the order in which the s02 frames arrived; and which partition two antennas map to when the partition count changes.A partition is the unit of parallelism
Within a consumer group, each partition is read by exactly one consumer. So a group can't have more active consumers than partitions: with three partitions, three consumers work, while the fourth, c4, gets no partition and reads nothing. It isn't broken; it's standing by. If one of the other three fails, its partition can be assigned to c4. Want four consumers working at once? You need a fourth partition. Writes can run in parallel too: partitions live on different brokers, and producers can write to them at the same time.
How a key chooses a partition
A message can have a key — in our case, the antenna name. The producer hashes the key, producing a number that is always the same for that key, then takes the remainder after dividing by the partition count. You can see three consequences in the cell output:
- one key always maps to one partition. Every
s02frame is in partition 0, and the consumer received them in the order they were sent: 0, 1, 2, 3; - different keys land in partitions according to their hashes. The hash knows nothing about load: the twelve antennas split five, two, and five, so
c2gets eight frames per second whilec1andc3get twenty each; - change the partition count, and keys move. With three partitions,
s03lands in partition 0; with four, in 2; with twelve, in 6. That's why partitions aren't added casually along the way: you plan the count in advance. We'll do that in “How Many Partitions: Calculate, Don’t Guess.”
Kafka guarantees order only within a partition. There is no ordering across partitions: a consumer may read a message from partition 2 before an older message from partition 0.
Without a key, there is no order
If there is no key, the producer chooses partitions on its own to keep the load more even: our training Kafka uses round-robin distribution, while real clients may distribute messages randomly or by batch. A batch is a group of messages sent together; the whole batch goes to one partition, and the next may go to another. The load becomes more even, but messages from one antenna can end up in different partitions, where there is no shared ordering. The reading room's proposal to “remove the key” fixes the load and breaks the recording.
Choose a key by asking, “Where do I need order?” If order matters per antenna, use the antenna as the key. If it matters per order, use the order number. If you don't need ordering at all, you can omit the key. But dropping the key isn't the right fix for the uneven 20, 8, and 20 split: later tonight the chapter will calculate how many partitions we need, and chapter 2 will explain why keys can still distribute unevenly.
signal_raw. The key was left out, so the reading room receives frames from the same antenna out of order.
Fix send_buffer(producer, antenna, frames): each antenna's frames must be read in the order they were sent, while the feed must still use all three partitions.
The replay_buffers(send_buffer) function is already available; its code is hidden. It creates signal_raw with three partitions, runs buffers from twelve antennas through your function, consumes the topic with the reading_room group, and returns two dictionaries: each antenna's frame numbers in read order, and the partitions where its frames landed. The last lines of the starter code print these values for s01 — you'll see the output when you click “Check.”What's real here, and what's simplified for the course
The rules are real: one key maps to one partition, order is preserved only within a partition, changing the partition count can move keys, and within a group each partition is read by one consumer.
Our training Kafka hashes keys with crc32 — the default used by librdkafka, which confluent-kafka-python is built on. The Java client and kafka-python use murmur2, so the antennas would land in different partitions rather than splitting 5, 2, and 5, but the result could still be uneven. Without a key, our training Kafka distributes messages round-robin; real clients may distribute them by batch or randomly. Either way, per-antenna order is lost. Our training Kafka lets us set the consumer name member_id to make the example easier to follow; in a real consumer group, Kafka assigns it when the consumer joins.
Interview question
How this comes up in interviews
“How does Kafka guarantee ordering?” Only within a partition. Messages that must stay ordered use the same key: the producer hashes the key modulo the partition count, so identical keys map to the same partition. A topic with multiple partitions has no global order.
“What happens if you add partitions?” Keys can move because the modulo operation now uses a different partition count. Old messages stay where they are, while new messages with the same key may go to another partition — so ordering for that key can break at the transition.
“How many consumers should you run in a group?” No more consumers can be active than there are partitions, because the partition is the unit of parallelism. Extra consumers stay idle until a partition becomes available.
The English terms are partition key, partitioner, and hot partition — a partition that receives too much of the load.
Key takeaways
| what | how it works | in our example |
|---|---|---|
| partition | within a group, one consumer reads it: no more consumers can be active than there are partitions | three partitions — c4 has nothing to do |
| key | hash the key modulo the partition count: one key maps to one partition | the antennas split 5, 2, 5 — that's 20, 8, and 20 frames per second |
| order | only within a partition | the s02 frames arrived as 0, 1, 2, 3 |
| no key | load is more even, but per-antenna order is lost | the s01 buffer was read as 0, 3, 1, 2 |
| partition count | change it, and keys can move | s03: partition 0 with three partitions, 6 with twelve |
Choose the key by asking, “Where do I need order?” Removing the key isn't the way to fix uneven load.
QUERY: We kept the key. The reading room grumbled, then agreed: the recording matters more.