0
0
Kafkadevops~10 mins

Consumer API basics in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Consumer API basics
Start Consumer
Subscribe to Topic
Poll for Messages
Process Messages
Commit Offsets
Repeat Polling
Back to Poll for Messages
The consumer starts, subscribes to a topic, polls messages, processes them, commits offsets, and repeats until stopped.
Execution Sample
Kafka
consumer = KafkaConsumer('my_topic')
consumer.subscribe(['my_topic'])
for message in consumer:
    print(message.value)
    consumer.commit()
This code creates a consumer, subscribes to 'my_topic', reads messages one by one, prints their values, and commits offsets.
Process Table
StepActionMessage PolledOffsetOutputCommit Offset
1Start Consumer and SubscribeNoneNoneNoneNone
2Poll for messageMessage10Print Message1Commit offset 1
3Poll for messageMessage21Print Message2Commit offset 2
4Poll for messageMessage32Print Message3Commit offset 3
5Poll for messageNo new message2No outputNo commit
6Stop ConsumerNone2NoneNone
💡 No new messages available, consumer stops polling.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
messageNoneMessage1Message2Message3NoneNone
offsetNone01222
committed_offsetNone12333
Key Moments - 3 Insights
Why do we commit offsets after processing each message?
Committing offsets tells Kafka which messages have been processed, so if the consumer restarts, it continues from the last committed offset (see steps 2-4 in execution_table).
What happens if no new messages are available during polling?
The poll returns no messages, so the consumer waits or stops polling (see step 5 in execution_table where no new message is polled).
Why do we subscribe to a topic before polling?
Subscription tells the consumer which topic(s) to listen to, so polling knows where to get messages from (see step 1 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the committed_offset after step 3?
A2
B1
C0
DNone
💡 Hint
Check the committed_offset value in variable_tracker after step 3.
At which step does the consumer find no new messages?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Message Polled' column in execution_table.
If we skip committing offsets, what would happen on restart?
AConsumer starts from next offset after last commit
BConsumer starts from last committed offset
CConsumer starts from beginning of topic
DConsumer cannot start
💡 Hint
Refer to key_moments about why committing offsets is important.
Concept Snapshot
Kafka Consumer API basics:
- Create consumer and subscribe to topic
- Poll messages in a loop
- Process each message
- Commit offsets after processing
- Repeat polling until stopped
- Committing offsets ensures no message loss or duplication
Full Transcript
This visual execution shows how a Kafka consumer works. First, the consumer starts and subscribes to a topic. Then it polls messages one by one. Each message is processed by printing its value. After processing, the consumer commits the offset to mark the message as done. This repeats until no new messages are available, then the consumer stops. Committing offsets is important to avoid reprocessing messages after restart. If no messages are found during polling, the consumer waits or stops polling. Subscribing to the topic tells the consumer where to get messages from.