0
0
Kafkadevops~5 mins

Consumer group concept in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
When multiple applications read messages from the same Kafka topic, they need a way to share the work without duplicating messages. Consumer groups let several consumers work together to read messages evenly and keep track of what has been read.
When you want to scale message processing by adding more consumers to share the load.
When you need to ensure each message is processed only once by one consumer in a group.
When you want to balance message consumption across multiple servers or instances.
When you want to track which messages have been processed to avoid duplicates.
When you want to handle failover so if one consumer stops, others continue processing.
Commands
Starts a consumer that reads messages from 'example-topic' as part of the 'my-consumer-group'. It reads all messages from the beginning of the topic.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --group my-consumer-group --from-beginning
Expected OutputExpected
This will print messages from the topic as they arrive, for example: Message 1 Message 2 Message 3
--group - Specifies the consumer group this consumer belongs to.
--from-beginning - Reads all messages from the start of the topic.
Shows details about the consumer group 'my-consumer-group', including which partitions each consumer is reading and the lag (messages not yet processed).
Terminal
kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-consumer-group
Expected OutputExpected
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID example-topic 0 15 20 5 consumer-1-12345 /127.0.0.1 consumer-1 example-topic 1 20 20 0 consumer-2-67890 /127.0.0.1 consumer-2
--describe - Shows detailed information about the consumer group.
--group - Specifies which consumer group to describe.
Key Concept

If you remember nothing else from this pattern, remember: consumer groups let multiple consumers share the work of reading messages from a topic without duplication.

Common Mistakes
Starting multiple consumers with different group names to read the same topic expecting load sharing.
Different groups each get all messages, so messages are duplicated instead of shared.
Use the same consumer group name for all consumers that should share the work.
Not specifying a consumer group when starting a consumer.
Without a group, the consumer acts alone and does not share or track offsets properly.
Always specify a consumer group with --group to enable coordinated consumption.
Assuming messages are deleted after consumption.
Kafka retains messages based on time or size, not consumption, so messages remain until retention expires.
Understand that consumer groups track offsets to know what is read, but messages stay in the topic.
Summary
Use the --group flag to assign consumers to the same group for shared message processing.
Check consumer group status and lag with kafka-consumer-groups --describe to monitor progress.
Consumer groups ensure each message is processed once by one consumer in the group.