0
0
Kafkadevops~10 mins

First message (produce and consume) in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - First message (produce and consume)
Start Producer
Create message
Send message to Kafka topic
Message stored in topic partition
Start Consumer
Poll topic for messages
Receive first message
Process and print message
End
The producer creates and sends a message to a Kafka topic. The consumer then polls the topic, receives the first message, and processes it.
Execution Sample
Kafka
from kafka import KafkaProducer, KafkaConsumer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('my_topic', b'Hello Kafka')
producer.flush()

consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092', auto_offset_reset='earliest', consumer_timeout_ms=1000)
for msg in consumer:
    print(msg.value.decode())
    break
This code sends a single message 'Hello Kafka' to 'my_topic' and then consumes the first message from that topic, printing it.
Process Table
StepActionEvaluationResult
1Start ProducerProducer connected to localhost:9092Ready to send messages
2Create messageMessage content: b'Hello Kafka'Message prepared
3Send messageSend to topic 'my_topic'Message sent to Kafka broker
4Flush producerEnsure message is sentMessage committed to topic partition
5Start ConsumerConsumer connected to localhost:9092Ready to poll messages
6Poll topic 'my_topic'Check for messages from earliest offsetMessage received: b'Hello Kafka'
7Process messageDecode bytes to stringOutput: 'Hello Kafka'
8Break loopStop after first messageConsumer stops polling
9EndNo more messages or stoppedProgram ends
💡 Consumer stops after receiving and processing the first message.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
producerNoneKafkaProducer instanceKafkaProducer instanceKafkaProducer instanceKafkaProducer instance
messageNoneb'Hello Kafka'b'Hello Kafka'b'Hello Kafka'b'Hello Kafka'
consumerNoneNoneNoneKafkaConsumer instanceKafkaConsumer instance
msg.valueNoneNoneNoneb'Hello Kafka'b'Hello Kafka'
outputNoneNoneNoneNone'Hello Kafka'
Key Moments - 3 Insights
Why does the consumer use 'auto_offset_reset=earliest'?
Because the consumer needs to read the first message from the beginning of the topic. Without this, it might start reading only new messages sent after it starts, missing the first message (see execution_table step 6).
Why do we call 'producer.flush()' after sending the message?
Flush ensures the message is actually sent to Kafka before the program continues. Without flush, the message might stay in a buffer and not be sent immediately (see execution_table step 4).
Why does the consumer break after processing one message?
To stop the loop after receiving the first message. Otherwise, the consumer would keep waiting for more messages indefinitely (see execution_table step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of 'msg.value' at step 6?
ANone
Bb'Hello Kafka'
CString 'Hello Kafka'
DEmpty bytes b''
💡 Hint
Check the 'msg.value' variable in variable_tracker after step 6.
At which step does the producer ensure the message is sent to Kafka?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the action 'Flush producer' in execution_table.
If we remove the 'break' in the consumer loop, what happens?
AConsumer keeps polling for more messages
BConsumer stops after first message anyway
CConsumer crashes immediately
DProducer sends more messages automatically
💡 Hint
See execution_table step 8 about breaking the loop.
Concept Snapshot
Kafka first message flow:
- Producer creates and sends message to topic
- Call producer.flush() to ensure send
- Consumer connects with auto_offset_reset='earliest'
- Consumer polls topic and receives messages
- Break loop after first message to stop
- Decode message bytes to string for use
Full Transcript
This example shows how a Kafka producer sends a first message to a topic and how a consumer reads that first message. The producer connects to the Kafka broker, creates a message 'Hello Kafka', sends it to the topic 'my_topic', and flushes to ensure delivery. The consumer connects to the same broker, subscribes to 'my_topic' with 'auto_offset_reset' set to 'earliest' so it reads from the beginning. It polls the topic, receives the first message, decodes it from bytes to string, prints it, and then breaks the loop to stop. Key points include flushing the producer to send messages immediately, setting the consumer to read from earliest offset, and breaking the consumer loop after the first message to avoid waiting forever.