This code sends a message 'Hello Kafka' to the topic 'topic1' and waits until it is sent.
Process Table
Step
Action
Evaluation
Result
1
Create message 'Hello Kafka'
Message ready
Message object created
2
Connect to Kafka broker
Connection successful
Producer connected
3
Send message to 'topic1'
Message accepted by broker
Message queued in broker
4
Flush producer
Wait for confirmation
Message confirmed sent
5
Message stored in topic
Message available for consumers
Message stored
💡 Message is published and stored in Kafka topic, ready for consumers
Status Tracker
Variable
Start
After Step 1
After Step 3
After Step 5
message
None
'Hello Kafka'
'Hello Kafka'
'Hello Kafka'
connection
Disconnected
Disconnected
Connected
Connected
broker_queue
Empty
Empty
Contains message
Contains message
Key Moments - 2 Insights
Why does the producer flush after sending?
Flushing ensures the message is actually sent to the broker and not just buffered locally, as shown in step 4 of the execution_table.
What happens if the broker is not connected?
The message cannot be sent or stored, so the connection must be successful before sending, as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the message after step 3?
AMessage queued in broker
BMessage confirmed sent
CMessage object created
DMessage available for consumers
💡 Hint
Check the 'Result' column in row for step 3 in execution_table
At which step does the producer confirm the message is sent?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look for 'Flush producer' and confirmation in execution_table
If the connection to broker fails, what happens to the message?
AMessage is stored anyway
BMessage is lost immediately
CMessage cannot be sent or stored
DMessage is sent to another topic
💡 Hint
Refer to step 2 and step 3 in execution_table about connection and sending
Concept Snapshot
Producer creates a message → Connects to Kafka broker → Sends message to a topic → Flushes to confirm sending → Broker stores message for consumers
Full Transcript
A Kafka producer creates a message and connects to the Kafka broker. It sends the message to a specific topic on the broker. The producer flushes to ensure the message is actually sent and confirmed. The broker stores the message so consumers can read it later.