0
0
Kafkadevops~5 mins

Consumer poll loop in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to read messages from a Kafka topic, you use a consumer that keeps checking for new messages in a loop. This loop is called the consumer poll loop. It helps your app get messages as they arrive without missing any.
When you want to process new messages from a Kafka topic continuously.
When you need to build a service that reacts to events in real time.
When you want to read messages in batches to improve efficiency.
When you want to commit your read position after processing messages.
When you want to handle message processing errors gracefully.
Commands
This command starts a Kafka consumer that connects to the Kafka server at localhost:9092, subscribes to 'example-topic', and joins the consumer group 'example-group'. It polls for messages and stops after 10 seconds of inactivity.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --group example-group --timeout-ms 10000
Expected OutputExpected
This is a sample message 1 This is a sample message 2 This is a sample message 3
--bootstrap-server - Specifies the Kafka server address to connect to.
--topic - Specifies the topic to consume messages from.
--group - Specifies the consumer group this consumer belongs to.
This command shows the current status of the consumer group 'example-group', including which partitions it is reading and the offset positions. It helps verify that the consumer poll loop is working and tracking progress.
Terminal
kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group example-group
Expected OutputExpected
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID example-group example-topic 0 15 15 0 consumer-1-12345-67890abcdef /127.0.0.1 consumer-1
--describe - Shows detailed information about the consumer group.
--group - Specifies the consumer group to describe.
Key Concept

If you remember nothing else from this pattern, remember: the consumer poll loop continuously checks for new messages so your app can process them in real time without missing any.

Common Mistakes
Not running the consumer in a loop and polling repeatedly.
Without a loop, the consumer reads messages only once and then stops, missing future messages.
Use a loop that calls poll() repeatedly to keep reading new messages as they arrive.
Not committing offsets after processing messages.
If offsets are not committed, the consumer may re-read the same messages after restart, causing duplicate processing.
Commit offsets manually or enable auto-commit to track progress and avoid duplicates.
Setting poll timeout too low or too high without testing.
Too low timeout causes frequent empty polls wasting resources; too high causes delays in processing new messages.
Choose a balanced poll timeout based on message frequency and processing speed.
Summary
Use the kafka-console-consumer command with --topic and --group to start a consumer that polls messages in a loop.
Check consumer group status with kafka-consumer-groups to verify message consumption and offset progress.
Remember to poll repeatedly and commit offsets to avoid missing or duplicating messages.