In the first lesson of this module we said real systems need two behaviours at once: every service gets every message, but within a service, each message is handled by only one of its instances. Kafka does both with one idea, the consumer group.
This lesson covers how a group shares a topic's partitions, why the number of partitions caps how far a group can scale, and what happens during the awkward few seconds when a consumer joins, leaves or dies.
One group per service
Every Kafka consumer belongs to a group, named by a group.id string. The rules are short:
- Every group gets every message in the topic. Groups don't affect each other at all.
- Within a group, each partition is assigned to exactly one consumer. That consumer reads all of that partition's messages, in order.
So the notifications service runs its instances with group.id = notifications, and the dispatch service uses group.id = dispatch. Both read the whole orders topic. Within notifications, the partitions are divided up among its instances.
A consumer group is a set of consumers that share a topic's partitions between them, each partition read by exactly one member, while every group gets every message.
Each group keeps its own offsets: notifications might be at offset 81,400 in partition 0 while dispatch is at 81,435. A slow group never holds up a fast one.