0
0
Kafkadevops~10 mins

Message key and value in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Message key and value
Create Message
Assign Key and Value
Send to Kafka Topic
Kafka stores message
Consumers read Key and Value
This flow shows how a Kafka message is created with a key and value, sent to a topic, stored, and then read by consumers.
Execution Sample
Kafka
producer.send(new ProducerRecord<>("my-topic", "user1", "Hello Kafka"));
Send a message with key 'user1' and value 'Hello Kafka' to the topic 'my-topic'.
Process Table
StepActionKeyValueTopicResult
1Create ProducerRecorduser1Hello Kafkamy-topicRecord ready to send
2Send messageuser1Hello Kafkamy-topicMessage sent to Kafka broker
3Kafka stores messageuser1Hello Kafkamy-topicMessage stored with key and value
4Consumer reads messageuser1Hello Kafkamy-topicConsumer receives key and value
💡 Message key and value successfully sent, stored, and consumed.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
keynulluser1user1user1user1
valuenullHello KafkaHello KafkaHello KafkaHello Kafka
topicnullmy-topicmy-topicmy-topicmy-topic
Key Moments - 2 Insights
Why do we need a key in a Kafka message?
The key helps Kafka decide which partition the message goes to and allows consumers to process messages with the same key in order, as shown in step 3 of the execution_table.
Can a Kafka message have a null key?
Yes, a message can have a null key, but then Kafka distributes messages round-robin across partitions. This is different from the example where the key is 'user1' (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the key value at Step 2?
Anull
B"Hello Kafka"
C"user1"
D"my-topic"
💡 Hint
Check the 'Key' column in row for Step 2 in execution_table.
At which step does Kafka store the message?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for the storing action.
If the key was null, how would the partitioning change?
AMessages are distributed round-robin across partitions
BMessages are sent to a single partition only
CMessages go to partitions based on key hash
DMessages are rejected by Kafka
💡 Hint
Refer to key_moments explanation about null keys and partitioning.
Concept Snapshot
Kafka messages have a key and a value.
The key determines the partition and ordering.
The value is the actual data sent.
Messages are sent to a topic.
Consumers read both key and value.
Null keys cause round-robin partitioning.
Full Transcript
This visual execution shows how a Kafka message is created with a key and value, sent to a topic, stored by Kafka, and then read by consumers. The key helps Kafka decide the partition and maintain order for messages with the same key. The value is the main content of the message. The execution table traces each step from creating the message record, sending it, storing it, and consuming it. Variables key, value, and topic are tracked through each step. Key moments clarify why keys are important and what happens if the key is null. The quiz tests understanding of key values at each step, when Kafka stores the message, and the effect of null keys on partitioning.