消费者组是 Kafka 的可扩展、容错消费机制——组中的消费者共享消费主题分区的工作,并自动进行重新平衡。深入理解它们是构建可扩展 Kafka 消费者的关键。
消费者组如何分配工作
A CONSUMER GROUP = consumers cooperating to consume a topic:
→ each PARTITION is assigned to exactly ONE consumer in the group (at a time)
→ partitions are distributed across the group's consumers → PARALLEL consumption
→ e.g. 6 partitions, 3 consumers → 2 partitions each
→ scale consumption by adding consumers (UP TO the partition count — extra consumers idle)
重新平衡
REBALANCING → redistributing partitions when group membership changes:
→ a consumer JOINS (scale up) or LEAVES/FAILS (scale down/crash) → partitions are
REASSIGNED among the current consumers
→ provides FAULT TOLERANCE (a failed consumer's partitions go to others) and elasticity
⚠️ during a rebalance, consumption PAUSES briefly (a cost); frequent rebalances hurt
→ minimize unnecessary rebalances; modern protocols reduce the impact (cooperative
rebalancing)
