0
0
Kafkadevops~10 mins

Consumer poll loop in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Consumer poll loop
Start Consumer
Poll for messages
Are messages received?
NoWait and Poll again
Yes
Process each message
Commit offsets
Loop back to Poll
The consumer starts, polls messages in a loop, processes them if any, commits offsets, then polls again.
Execution Sample
Kafka
consumer = KafkaConsumer(bootstrap_servers=['localhost:9092'], enable_auto_commit=False)
consumer.subscribe(['topic'])
while True:
    records = consumer.poll(timeout_ms=1000)
    for record_list in records.values():
        for record in record_list:
            process(record)
    consumer.commit()
This code continuously polls messages from a Kafka topic, processes them, and commits offsets.
Process Table
StepActionPoll ResultProcess MessagesCommit OffsetsLoop Back
1Start consumer and pollNo messagesNo processingNo commitPoll again
2Poll messagesReceived 2 messagesProcess message 1No commit yetContinue processing
3Process message 2Same 2 messagesProcess message 2No commit yetAfter processing all
4Commit offsetsNo new pollNo processingOffsets committedPoll again
5Poll messagesNo messagesNo processingNo commitPoll again
6Poll messagesReceived 1 messageProcess message 1No commit yetContinue processing
7Commit offsetsNo new pollNo processingOffsets committedPoll again
8Poll messagesNo messagesNo processingNo commitPoll again
ExitStop condition met (not shown in code)Stop pollingStop processingStop committingExit loop
💡 Loop runs indefinitely unless stopped externally; here exit is hypothetical.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
recordsemptyempty2 messages2 messagesemptyempty1 messageempty
processed_messages00122211
offsets_committedfalsefalsefalsefalsetruetruetruetrue
Key Moments - 3 Insights
Why does the consumer poll again even if no messages are received?
Because the poll loop is continuous; when poll returns no messages (see Step 1 and 5), the loop waits and polls again to check for new messages.
When are offsets committed in the loop?
Offsets are committed only after all messages from the current poll are processed (see Step 4 and 7), ensuring processed messages are marked.
Does the poll method return messages immediately every time?
No, poll waits up to the timeout and may return no messages if none are available (see Step 1 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'processed_messages' after Step 3?
A2
B1
C0
D3
💡 Hint
Check the variable_tracker row for 'processed_messages' at After 3.
At which step does the consumer commit offsets for the first time?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Commit Offsets' column in the execution_table.
If the poll always returned no messages, how would the 'processed_messages' variable change?
AIt would keep increasing
BIt would reset to 0 each loop
CIt would stay at 0
DIt would become negative
💡 Hint
Refer to variable_tracker and execution_table rows where poll returns no messages.
Concept Snapshot
Consumer poll loop syntax:
while True:
  records = consumer.poll(timeout_ms=1000)
  for record_list in records.values():
    for record in record_list:
      process(record)
  consumer.commit()

Behavior:
- Poll waits for messages
- Processes all received messages
- Commits offsets after processing
- Loops continuously to consume new messages
Full Transcript
This visual execution shows how a Kafka consumer uses a poll loop to receive messages. The consumer starts and polls for messages. If no messages arrive, it polls again. When messages arrive, it processes each one, then commits offsets to mark progress. This repeats indefinitely. Variables like 'records', 'processed_messages', and 'offsets_committed' change step by step. Key moments clarify why polling repeats without messages and when commits happen. The quiz tests understanding of these steps and variable changes.