23. Pub/Sub and Fan-Out
23.3 What are consumer groups?
On the paper round: The Hindu has a thousand subscribers in one area. One delivery boy can't do them all, so four boys share the area. Each house still gets one copy, not four. Meanwhile, a separate team delivers Sportstar to its own subscribers.
In the system: A consumer group (Kafka), or a subscription with several workers (Google Pub/Sub, SQS behind SNS), lets several instances of one service share the messages of a subscription:
- Between groups: every group gets every message (pub/sub).
- Inside a group: each message goes to one member (a queue).
Figure: pub/sub between groups, a queue inside each group.
In the chapter's code, 10 "order placed" events reach the loyalty and analytics groups in full, while the SMS group's two workers get 5 each, together covering all 10.
What this gives you:
- Scale a service by adding members to its group.
- Add a new service by adding a new group; nobody else changes.
- In Kafka, a group's members split the partitions, so a topic's partition count caps how many members can work at once (Chapter 22).
Memory code: "Every team gets the paper; inside a team, the boys split the street."