In this lesson · topic 2 of 7
Topic 2
Partitions, offsets and consumer groups
6 min read
From a key to a consumer
Step through one message, from the producer to a consumer and through a failure.
From a key to a consumer
Step 1 / 6- key
- user-7
- partitions
- 4
A producer sends a keyed message
The message carries a key, here "user-7", which is whatever should stay in order: a user, an order, a device. The topic has 4 partitions, each an ordered log. The offsets in the picture are made up for the example.
The key picks the partition
The producer hashes the key and takes the result modulo the number of partitions. The same key always lands in the same partition, which is what keeps its messages together.
The message is appended
It goes on the end of that partition's log and gets the next offset, 40. Offsets only grow. Nothing is inserted in the middle or moved.
One consumer owns each partition
A consumer group of two shares the four partitions. Each partition has exactly one owner inside the group, so the owner reads it in order and nobody competes for its messages.
The consumer commits its offset
Saving progress on every message would be slow, so the consumer saves it now and then. The committed offset is where a restarted reader will begin. The position can be ahead of it.
A consumer fails and the group rebalances
C2 dies. The group stops, the partitions are dealt out again, and C1 takes them all. It starts each one at the committed offset, so the two messages after the commit are processed a second time.
- A producer sends a keyed message. The message carries a key, here "user-7", which is whatever should stay in order: a user, an order, a device. The topic has 4 partitions, each an ordered log. The offsets in the picture are made up for the example. key: user-7; partitions: 4.
- The key picks the partition. The producer hashes the key and takes the result modulo the number of partitions. The same key always lands in the same partition, which is what keeps its messages together. chosen partition: P0.
- The message is appended. It goes on the end of that partition's log and gets the next offset, 40. Offsets only grow. Nothing is inserted in the middle or moved. offset: 40.
- One consumer owns each partition. A consumer group of two shares the four partitions. Each partition has exactly one owner inside the group, so the owner reads it in order and nobody competes for its messages. C1 owns: P0 P1; C2 owns: P2 P3.
- The consumer commits its offset. Saving progress on every message would be slow, so the consumer saves it now and then. The committed offset is where a restarted reader will begin. The position can be ahead of it. committed: 41; position: 43.
- A consumer fails and the group rebalances. C2 dies. The group stops, the partitions are dealt out again, and C1 takes them all. It starts each one at the committed offset, so the two messages after the commit are processed a second time. re-read: 2 messages; owner of all: C1.
The log
A topic is a named stream. It is split into partitions. Each partition is an append-only log: new messages go on the end, and nothing is changed or reordered afterwards. The position of a message in its partition is its offset. The first message has offset zero, the next has one, and so on. An offset is an address that never changes.
Because the log is kept after a message is read, reading does not remove anything. A consumer only keeps track of where it is. That is different from a queue that deletes a message when it is acknowledged.
Order
The log gives one guarantee about order: messages in the same partition are read in the order they were appended. Since the key decides the partition, messages with the same key are in order.
Across partitions there is no order. Two messages with different keys can be read in either order, and which comes first depends on which consumer is quicker. The original Kafka paper states the same limit. Messages from a single partition arrive in order, and there is no guarantee between different partitions.
Consumer groups
A consumer group is a set of consumers that share a topic. The group deals out the partitions. Each partition has one owner at a time, and one consumer can own several partitions.
This is why the number of partitions caps the parallelism of a group. A group of more consumers than partitions leaves the extras with nothing to read.
Offsets, commits and replay
A consumer keeps a position, the offset of the next message it will read. Now and then it saves that to a durable place as the committed offset. If the consumer fails and another takes its partitions, the new owner starts at the committed offset. Whatever the old consumer processed since the last commit is read again.
That makes the log at-least-once by default, which is the previous lesson’s trade. A consumer that cares has to be idempotent.
The same mechanism gives replay. Because messages stay in the log, a consumer can move its position back and read them again: to rebuild a table after a bug fix, to feed a new service the history, or to recover from bad data. The only limit is retention. A log keeps messages for a time, or up to a size, and then deletes the oldest ones.
Rebalance
When a consumer joins, leaves or fails, the group has to deal out the partitions again. This is a rebalance. In the simple protocol every consumer stops reading while it happens, and each partition resumes from its committed offset. During the pause the producers keep writing, so lag builds up and has to be cleared afterwards.