Partitioned logs and consumer groups
A log, cut into partitions
Section titled “A log, cut into partitions”A work queue hands each message to one consumer and then forgets it. A log-based broker, of which Kafka is the best known, works differently: a topic is split into partitions, each partition is an ordered log to which messages are appended, and a consumer reads a partition by keeping track of a position in it, the offset. In the lab, reading does not remove anything from the log, and the next lesson is about what that allows.
Why split a topic? One log on one machine has a limit for how fast it can be written and read. Kafka’s introduction describes a topic as spread over a number of buckets located on different brokers, which is what lets it scale. Each partition is an independent log, so the partitions of a topic can be written and read in parallel.
Order, but only inside a partition
Section titled “Order, but only inside a partition”Which partition does a message go to? Kafka’s introduction says that events with the same event key, such as a customer or vehicle identifier, are written to the same partition, and that any consumer of a given partition reads its events in exactly the order in which they were written. The key is the producer’s way to say what must stay in order: all the events of one customer go to one partition, so they are read in order.
The guarantee has a limit that the documentation states by its wording: the order is per partition. Between two partitions there is no order. A consumer that reads customer-7’s events sees them in the order they were written, and it may see them before or after customer-8’s events written earlier.
The lab chooses the partition of a message by hashing its key, key-0 to key-63, with a hash mod the number of partitions. The hash is the lab’s choice and not what a real broker uses, and it does not split the 64 keys perfectly: with 4 partitions they get 15, 17, 17 and 15 keys, which is 23%, 27%, 27% and 23% of the messages when every key is as likely as any other.
A group shares the partitions
Section titled “A group shares the partitions”Several consumers can read a topic together as a consumer group. Kafka’s design documentation says that each partition is consumed by exactly one consumer within each subscribing consumer group at any given time. So inside a group the partitions are shared out: with 4 partitions and 2 consumers, each consumer reads 2. The group’s capacity is the sum of its consumers’ rates, as in the work queue, but only for the consumers that own a partition.
It also gives the log broker both of the shapes of the first lesson. One group is a work queue: the group’s consumers share the messages. Two groups are pub/sub: each group receives every message, with a position of its own, and a new group does not take anything away from the first.
Mental model. A partition is a lane, and a lane has one reader per group. The number of partitions sets the most parallelism a group can use, and the key decides which lane a message takes, so one very popular key makes one lane long.
Try it: add consumers, then break the balance
Section titled “Try it: add consumers, then break the balance”The lab opens on a topic of 4 partitions and a group of one consumer. Producers send 12 messages a second, a consumer processes 5, and the topic has the lag that results. Read Consumers, Messages with the hot key and the status sentence, and the table of partitions.
A topic is split into partitions, and a message goes to the partition its key hashes to. Add consumers to the group and watch the partitions being shared out.
What is happening
Producers send 12 a second and the 1 consumers at work can process 5. The lag grows by about 7 a second.
- Consumer lag
- 7018 on Partition 1, the furthest behind
- Messages processed
- 5050 different messages, 0 read again
- Consumers at work
- 1 of 1each partition is read by one consumer of the group
- Rebalances
- 0each pauses the whole group for 2 s
Total lag Furthest behind
| Partition number | Read by | Log end | Read up to | Committed | Lag | Share of messages |
|---|---|---|---|---|---|---|
| Partition 0 | Consumer 1 | 30 | 13 | 13 | 17 | 23% |
| Partition 1 | Consumer 1 | 38 | 20 | 20 | 18 | 27% |
| Partition 2 | Consumer 1 | 32 | 14 | 14 | 18 | 27% |
| Partition 3 | Consumer 1 | 20 | 3 | 3 | 17 | 23% |
Offsets count messages from 0: "Read up to" is the offset the consumer reads next, and "Committed" is where a new owner would start.
Last messages processed
Within a partition the offsets only go up. Between partitions there is no order at all.
- Partition 0, offset 11
- Partition 1, offset 19
- Partition 0, offset 12
- Partition 2, offset 9
- Partition 2, offset 10
- Partition 2, offset 11
- Partition 2, offset 12
- Partition 2, offset 13
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
- A message goes to the partition its key hashes to: FNV-1a of the key text mod the number of partitions. There are 64 keys, and the hot key is key-0. The hash is a choice of the lab, not what any particular broker uses.
Simplifications
- Partitions are shared out in consecutive ranges, and a change of the group pauses all consumers for two seconds, as an eager rebalance does. A crashed consumer loses what it processed since the last commit; the commit interval is a setting.
- Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
At the start, after 10 s, the lag is 70 messages, and the status says that it grows by about 7 a second: 12 in, 5 out. Press Skip 30 s twice, to 70 s: 490. One consumer reads all four partitions and cannot keep up.
Press Reset to go back to 10 s, then Add a consumer until there are 4. The group goes from 1 to 4 consumers, commits its offsets and pauses for 2 s to rebalance, and then 4 consumers can process 20 a second, more than the 12 sent, so the lag does not grow. Press Skip 30 s twice and the lag is 0 at 70 s. The partitions are now shared one to a consumer.
Add two more consumers, 6 in all. The status says that 2 consumers own no partition and sit idle: a partition is read by one consumer of the group at a time, so consumers beyond the number of partitions add no capacity. They are not broken, they have nothing to read. Capacity is still 20 a second.
Press Reset again, set Consumers to 4 and Messages with the hot key to 60%. The other messages carry any of 64 keys, and these 60% all carry key-0, so they all go to one partition. The status says that Partition 1 receives 71% of the messages, 8.5 a second, but that its one consumer can process 5. Press Skip 30 s twice, and at 70 s the lag is 233, all of it on that partition, and the other three are empty. Do the same with Consumers set to 8 instead of 4 and the lag is again 233: the extra consumers have no partition to take, and the one that owns the hot partition cannot be helped by another, because a partition is read by one consumer at a time.
What happens if the partitions are too few?
Section titled “What happens if the partitions are too few?”The number of partitions is a ceiling on the parallelism of every group that reads the topic. A topic with 4 partitions cannot be read faster than by 4 consumers. If the work per message is slow and the group needs 8 consumers, the fix is not more consumers but more partitions, and in the lab, changing the number of partitions starts the topic again because every key may now go to a different partition. In the lab that is a reset. For ordering it is a real cost: the order of a key is only kept inside one partition, so when keys move, the order across the move is not guaranteed by the partitioning alone.
A hot key is the other ceiling. It is the same problem as the hot shard of the partitioning module: one key is one key, it lives in one partition, and a partition has one reader per group. The key can be split into several, at the price of the order between them.
- A log-based broker splits a topic into partitions, each an ordered log, read by offset. Messages with the same key go to the same partition.
- Order is guaranteed inside a partition and not between partitions.
- In a consumer group each partition is read by exactly one consumer. One group is a work queue, several groups are pub/sub.
- One consumer against 12 in and 5 out gives a lag of 490 at 70 s. With 4 consumers the lag drains, with 6 two sit idle.
- A hot key puts 71% of the messages on one partition, 8.5 a second against 5, and 8 consumers change nothing: the lag stays at 233.
Next: offsets and replay, what happens to a group when a consumer crashes.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.