Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1 · One topic, many readers: why consumer groups exist

In one sentence: a consumer group is a set of consumers that share out a topic’s partitions, so that each partition is read by exactly one member, and that keeps a bookmark per partition so the group carries on where it stopped.

The problem it solves

Your shop writes every order to a Kafka topic called orders, 200 a second. A billing service reads them and charges each customer. Charging takes 10 ms an order, so one copy of the service can do at most about 100 a second.

In this course’s run, a consumer that spends 10 ms on each record was 2,259 orders behind after 20 seconds and 9,313 behind after 80: about 118 more every second.

A second copy, as just another consumer, reads every order too: each customer is charged twice. What you need:

  1. copies that split the orders, each order handled once;
  2. a copy that restarts carries on where it was;
  3. a second team (shipping) reading the same orders without taking any from billing.

The idea: share out partitions, keep a bookmark

A topic is already split. orders was created with 6 partitions: six separate, ordered logs. Each record the producer sends goes into one of them and gets the next number in that partition, its offset (0, 1, 2 …).

A group shares out the partitions, not the records. Consumers that start with the same group.id form a consumer group. Kafka gives each of the 6 partitions to exactly one member of the group. With two members, each gets three; with three, two each. A member reads only its own partitions, so no order is read twice inside the group.

 topic orders           group billing (3 members)       group shipping (1 member)
 partition 0  ───────▶  b1                              s1 reads all 6
 partition 1  ───────▶  b1
 partition 2  ───────▶  b2
 partition 3  ───────▶  b2
 partition 4  ───────▶  b3
 partition 5  ───────▶  b3

The slow consumer from above, run as three members of one group, was under 900 orders behind at each of four checks, 20 seconds apart (899, 393, 716 and 888): each member had on average a third of the orders, about 67 a second, under its limit of 100.

Groups don’t see each other. shipping gets its own share-out of the same 6 partitions: Kafka doesn’t delete a record when it’s read, so every group reads everything.

A bookmark per partition. Each group remembers, for each partition, the offset of the next record it should read: the committed offset. A member that restarts asks for it and continues from there. Lesson 3 is all about when that bookmark is written and what that guarantees.

What it rests on

Three pieces you use without seeing them:

PieceWhat it is
The poll loopA consumer calls poll() over and over; each call returns the next records from its partitions. Joining, leaving and committing happen inside those calls.
The group coordinatorOne broker per group keeps its member list and hands out partitions (lesson 2).
__consumer_offsetsThe internal topic where coordinators write every group’s bookmarks.

What it looks like

Six hundred orders, then two members of billing and one of shipping, each printing which partitions it was given and how much it read (this is the Watcher program you write in Practical 0):

05:12:17.507 b1 assigned [0, 1, 2]  now [0, 1, 2]
05:12:18.014 b1 read 296 from [1]
05:12:17.505 b2 assigned [3, 4, 5]  now [3, 4, 5]
05:12:18.100 b2 read 304 from [4, 5]
05:12:14.476 s1 assigned [0, 1, 2, 3, 4, 5]  now [0, 1, 2, 3, 4, 5]
05:12:15.069 s1 read 600 from [1, 4, 5]

billing split the 600 (296 + 304), and shipping read all 600. The even split was luck: a producer sending records with no key sticks to one partition until it has sent batch.size bytes there, so this quick burst landed in only three partitions. KIP-480 introduced the idea; since Kafka 3.3 the built-in partitioner works as KIP-794 describes, also sending less to slower brokers (partitioner.adaptive.partitioning.enable):

orders:0:0
orders:1:296
orders:2:0
orders:3:0
orders:4:296
orders:5:8

b1 owned three partitions but only one had records. A group balances partitions, not work.

After the members stopped, the group still exists, with its bookmarks:

./kafka kafka-consumer-groups --describe --group billing
Consumer group 'billing' has no active members.

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
billing         orders          1          296             296             0               -               -               -
billing         orders          4          296             296             0               -               -               -
billing         orders          5          8               8               0               -               -               -
...

CURRENT-OFFSET is the bookmark, LOG-END-OFFSET the offset the next new record will get, and LAG the difference: how many records the group has still to read. A new member of billing started next read 0 records: the bookmarks were at the end. A member of a new group, audit, read all 600.

Try it on your computer

Practical 0, after lesson 2, has you start a three-broker cluster with ./lab setup p0, write that Watcher yourself, and watch a group share out its partitions as members join and leave.

Go deeper: the bookmarks' own topic

__consumer_offsets is an ordinary topic with special settings. On this course’s cluster:

Topic: __consumer_offsets	TopicId: VIp08GAvRyiKzMTuVthjwQ	PartitionCount: 50	ReplicationFactor: 3	Configs: compression.type=producer,min.insync.replicas=1,cleanup.policy=compact,segment.bytes=104857600,retention.bytes=8388608

(retention.bytes is the lab’s size cap, set on every broker; it applies only to topics that delete old data, not to a compacted one like this.)

  • 50 partitions (offsets.topic.num.partitions): each group’s bookmarks all go to one of them, picked from the group’s name. Lesson 2 uses that to find the coordinator.
  • 3 replicas: a copy on each broker, so losing one broker loses no bookmarks.
  • cleanup.policy=compact: each commit is a record whose key is (group, topic, partition). Compaction keeps only the newest record per key, so the topic stays small however often groups commit.

The topic isn’t there from the start: on the fresh cluster, before orders was even created, ./kafka kafka-topics --list printed nothing at all; after this lesson’s runs it listed __consumer_offsets and orders. Reference: Kafka 4.4 broker configs (offsets.topic.*).

Common misunderstandings

  • “More consumers always read faster.” Only up to the number of partitions. A partition belongs to one member of a group: when seven members joined one group on orders, the seventh was given no partition at all.
  • “The consumer group is the running processes.” The group outlives them: it lives on the coordinator, with its bookmarks, even with no members, as the describe above shows.

Check yourself

1. Billing runs 4 members on orders (6 partitions). How many partitions does each get? Two members get 2 and two get 1: every partition has exactly one owner, and 6 doesn't divide by 4. The members with 2 partitions do twice the work if the orders are spread evenly. Lesson 2 shows which members get which.
2. You stop all of billing for an hour, then start it again. Where does it start reading? At its bookmarks, the committed offsets: the records produced during that hour are its lag, and it reads them first. Nothing is skipped and nothing done before the stop is read again (as long as the bookmarks were up to date when it stopped: lesson 3).
3. Billing and a new fraud-check service both need every order. Same group or different groups? Different groups. In the same group they would split the partitions, and each would see only part of the orders. In different groups, each gets its own share-out and its own bookmarks.

Next: 2 · Who gets which partition: assignment and rebalancing.