Explain the concept of Consumer Groups in Kafka and how they help in scaling.

Instruction: Provide a detailed explanation of what consumer groups are, their role in Kafka, and how they contribute to scaling and message consumption.

Context: This question assesses the candidate's understanding of one of Kafka's core scalability features. It tests knowledge on consumer groups, their operation, and their impact on message consumption scalability and parallelism.

Updated

Official answer

A standard subscribing Kafka consumer group uses a common group.id to divide subscribed partitions among its members. During a stable assignment, each partition has one owner within that group. Another group can independently consume the same retained topic and maintain its own progress.

For a topic with six partitions, three equally subscribed consumers might own two partitions each. Eight such consumers cannot all receive a partition from that topic; at least two are idle for that topic. Actual assignment also depends on subscriptions and the assignor, and a hot partition can dominate the workload. These are standard KafkaConsumer group semantics, distinct from Kafka share groups.

Membership changes or new partitions can trigger reassignment. Poll regularly, bound processing time and handle revoked/lost partitions so unfinished work and stale workers do not corrupt progress. KafkaConsumer is not generally thread-safe; one consumer-owning thread should manage polling and commits.

A committed offset identifies the next position from which processing should resume, not the last completed record. For example, after completing delivered offsets 10 and 12, with the next delivered record at 15 still unfinished, commit 15. The gap at 11 is not an error: offsets need not be consecutive. Do not commit past an unfinished delivered record just because later work completed.

Processing before committing can replay work after a crash; committing before processing can lose work. If work is delegated to other threads while polling continues, disable automatic commits and coordinate completion explicitly. Group ownership alone does not provide exactly-once external side effects.

Measure lag and completion throughput before scaling, and check partition count, skew and downstream capacity. More members do not guarantee linear speedup.

Consumer groups and commits, rebalance protocol

Related Questions