Consumer groups and scaling

Apache Kafka · Essentials · 12 min

Groups are how Kafka scales consumption and lets many applications read the same topic independently.

Every consumer belongs to a consumer group named by group.id. Kafka assigns each partition of the subscribed topics to exactly one consumer in the group, so the group as a whole processes every record once, in parallel.

  • Start a second instance of the same service with the same group.id, and partitions are redistributed between the two: a rebalance.
  • If an instance dies, its partitions move to the survivors.
  • A group can usefully have at most as many active consumers as partitions; extra consumers sit idle. Partition count therefore caps a group's parallelism.

Different groups are independent: the billing group and the analytics group each receive every record of orders, each with its own committed offsets. This is the publish/subscribe side of Kafka, and why adding a new consumer application is free for everyone else.

Committed offsets are stored per group and partition in the internal __consumer_offsets topic.

During a rebalance, consumption in the affected partitions pauses briefly. Frequent rebalances (instances crashing, or processing that takes longer than max.poll.interval.ms between polls) are a common source of duplicate processing and lag; later lessons cover how to avoid them.

Example

# topic "orders" has 3 partitions: P0, P1, P2
#
# group "billing", 1 instance:   A: P0 P1 P2
# group "billing", 2 instances:  A: P0 P1    B: P2          (rebalance)
# group "billing", 4 instances:  A: P0  B: P1  C: P2  D: idle
#
# group "analytics" (independent): X: P0 P1 P2   -> also sees every record
#
# Each (group, partition) pair has its own committed offset:
#   billing   P0 = 1042   analytics P0 = 977

Remember

  • Within a group, each partition is consumed by exactly one member
  • Max useful consumers in a group = number of partitions
  • Separate groups each read the full stream independently
  • Membership changes trigger rebalances

Watch out for

  • Starting 10 consumer instances for a 3-partition topic and wondering why 7 receive nothing.

Check yourself

  1. Topic payments has 4 partitions. Groups A (2 consumers) and B (1 consumer) subscribe. How many times is each record processed in total?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: kafka.apache.org/43/design/design/#the-consumer