Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. One line is not enough
  2. 2. Partitions, offsets and consumer groups
  3. 3. Four partitions, one hot key, one rebalance
  4. 4. When one partition cannot keep up
  5. 5. How many partitions to create
  6. 6. Where the commit goes decides the guarantee
  7. 7. Owners, order and commits: three checks

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
keyuser-7P036373839P133343536P238394041P335363738a small example with made-up offsets
key
user-7
partitions
4
  1. 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 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.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms