Consumer Groups

Parallel consumption, group coordination, rebalancing strategies, and scaling.

Beginner 25 min read 📨 Kafka

What Are Consumer Groups?

A consumer group is a set of consumers that cooperate to read from a topic. Kafka automatically distributes partitions among consumers in the group — each partition is assigned to exactly one consumer. This is how Kafka achieves parallel processing.

Think of it like a team of workers processing orders. Each worker (consumer) handles a subset of orders (partitions). If a worker goes home sick, their orders are redistributed to the remaining workers (rebalancing).

Key Rules

PartitionsConsumersAssignmentNote
632 partitions eachIdeal — even distribution
661 partition eachMaximum parallelism
686 active, 2 idleExtra consumers wasted
61All 6 partitionsNo parallelism
Kafka consumer group with partitions assigned to individual consumers
Consumer group partition assignment — Source: kafka.apache.org (Apache 2.0)

Monitoring Consumer Groups

# List all consumer groups
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

# Describe a group (shows lag per partition)
kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
  --describe --group my-app

# Reset offsets to earliest (reprocess all data)
kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
  --group my-app --topic user-events \
  --reset-offsets --to-earliest --execute
Output
GROUP   TOPIC        PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG   CONSUMER-ID
my-app  user-events  0          1042            1050            8     consumer-1
my-app  user-events  1          998             998             0     consumer-2
my-app  user-events  2          1105            1200            95    consumer-3

The LAG column is the most important metric. High lag means consumers can't keep up with producers. If lag keeps growing, you need to add more consumers (up to the partition count) or optimize processing speed.

Key Takeaway: Monitor consumer group lag continuously. Alert when lag exceeds a threshold. Adding consumers beyond the partition count won't help — add partitions first, then consumers.

⚠️ Common Mistake: More consumers than partitions

If you have 3 partitions and scale to 6 consumers, 3 consumers sit completely idle. Always ensure partition count ≥ consumer count. Scale partitions first.

Consumer Group: 3 Consumers, 6 Partitions
Consumer 1
P0, P1
Consumer 2
P2, P3
Consumer 3
P4, P5

Practice Exercises

Easy Hello World Variant

Modify the example to accept user input and print a personalized greeting.

Easy Code Reading

Read through the code examples above and predict the output before running them.

Medium Extend the Example

Take one code example and add error handling, input validation, or a new feature.