Lesson 2 · About 15 minutes
Who reads which partition?
By the end, you can look at a group, its consumers and its topics, and work out who reads what, who sits idle, and what forces a reshuffle.
Lesson 1 decided where an event lands. This lesson decides who reads it.
The four rules
How the default assignor divides partitions
By default, the client's partition.assignment.strategy is RangeAssignor,CooperativeStickyAssignor, and Range is the one used. Range works one topic at a time: sort the consumers by name, divide the partitions, and give any remainder to the first consumers. Because it does this for each topic, the same consumer gets the extra partition every time. Apache's example has 2 consumers and 2 topics of 3 partitions each:
C0: t0-0, t0-1, t1-0, t1-1 (4 partitions)
C1: t0-2, t1-2 (2 partitions)
group.protocol=consumer) moves assignment to the broker and doesn't use partition.assignment.strategy. In 4.4 the client default is still classic, which is what this lesson teaches. Share groups are also separate: they can have more consumers than partitions, so rule 2 applies only to ordinary consumer groups.Try it: predict, then click
Partitions here are assigned with Range. Decide the answer before each click.
billinghas 2 consumers on 3 partitions. Who has the extra one, and why?- Add two consumers to
billing. Who ends up idle? - Add a consumer to
analytics. Doesbilling's rebalance count change? - Stop the first consumer in
billing. Does the idle consumer get work? Do the others keep their partitions? - Click Reset, then Add topic refunds. How many partitions does each consumer have now?
Now on a real broker
Run this from the learn-kafka folder with Docker running. Start four consumers on three partitions, then look at who owns what.
source assets/kafka-lab.sh
kafka_lab_up
k kafka-topics.sh --create --topic orders \
--partitions 3 --replication-factor 1
for c in c1 c2 c3 c4; do k_consumer_start $c billing orders; done
sleep 12
k_members billing
Now stop c1. Before running this, predict who picks up its partition.
k_consumer_stop c1
sleep 8
k_members billing
Send three events, then read them with a brand-new group. It should get all three, even though billing has already read them.
printf 'alice:o1\nbob:o2\ncarol:o3\n' | \
k kafka-console-producer.sh --topic orders \
--reader-property parse.key=true --reader-property key.separator=:
k kafka-console-consumer.sh --topic orders --group analytics \
--from-beginning --timeout-ms 6000 2>/dev/null
Finally, reproduce the uneven Range split with two topics and two consumers.
k kafka-topics.sh --create --topic refunds \
--partitions 3 --replication-factor 1
k_consumer_start a shipping 'orders|refunds'
k_consumer_start b shipping 'orders|refunds'
sleep 12
k_members shipping
kafka_lab_down
You should see c4 idle at first, then working after c1 leaves. c2 and c3 also change partitions, even though they never left. At the end, a has 4 partitions and b has 2. To see the real tool underneath k_members, run kafka-consumer-groups.sh --describe --group billing --members --verbose through k.
Check yourself
Answer from memory. Two questions go back to Lesson 1 on purpose, because returning to old material after a gap is what makes it last.
Keep going
Read next: the "Consumer Groups and Topic Subscriptions" section of the KafkaConsumer Javadoc. It comes from the project itself, and it also previews the next lesson.
Keep for review: Consumer groups cheat sheet and Keys and partitions cheat sheet.
Ask your teacher: anything unclear is worth asking about in the chat, however small. When you finish, send your quiz score, which questions you missed, and whether you ran the broker lab.