0
0
Kafkadevops~10 mins

Partition concept in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Partition concept
Message Produced
Partitioner Decides Partition
Message Stored in Partition
Consumer Reads from Partition
Message Processing
A message is produced, the partitioner decides which partition it goes to, the message is stored there, and consumers read messages from partitions for processing.
Execution Sample
Kafka
producer.send(topic, key, value)
// Partitioner uses key to pick partition
// Message stored in that partition
// Consumer reads from assigned partitions
This code sends a message with a key to a Kafka topic; the partitioner uses the key to decide the partition where the message is stored; consumers read messages from partitions.
Process Table
StepActionKeyPartition ChosenMessage StoredConsumer Reads From
1Produce messageuser123Partition 2Partition 2Partition 2
2Produce messageuser456Partition 0Partition 0Partition 0
3Produce messageuser789Partition 1Partition 1Partition 1
4Produce messageuser123Partition 2Partition 2Partition 2
5Produce messagenullPartition 0 (round-robin)Partition 0Partition 0
6Stop producing----
💡 No more messages to produce; processing stops.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
KeyN/Auser123user456user789user123nullnull
Partition ChosenN/A201200
Messages in Partition 00011122
Messages in Partition 10001111
Messages in Partition 20111222
Key Moments - 2 Insights
Why does the same key always go to the same partition?
Because the partitioner uses the key's hash to pick a partition, so the same key maps to the same partition every time, as shown in steps 1 and 4 in the execution_table.
What happens when the key is null?
When the key is null, Kafka uses round-robin to assign partitions, so messages without keys are distributed evenly, as seen in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which partition does the message with key 'user789' go to?
APartition 0
BPartition 1
CPartition 2
DPartition 3
💡 Hint
Check step 3 in the execution_table under 'Partition Chosen'.
At which step does the partitioner assign a partition using round-robin because the key is null?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the row where the key is 'null' in the execution_table.
If the key 'user123' changed to 'user999', what would likely happen to the partition chosen?
AIt would change to a different partition
BIt would stay the same partition 2
CIt would cause an error
DIt would be assigned randomly every time
💡 Hint
Since partition depends on key hash, changing key changes partition, see variable_tracker for key and partition relation.
Concept Snapshot
Kafka Partition Concept:
- Messages are sent to topics divided into partitions.
- Partitioner uses message key to pick partition (same key → same partition).
- Messages without keys are assigned partitions round-robin.
- Consumers read messages from assigned partitions.
- Partitions enable parallelism and ordering per key.
Full Transcript
In Kafka, when a message is produced, the partitioner decides which partition the message goes to based on the message key. The same key always maps to the same partition, ensuring order for that key. If the key is null, Kafka assigns partitions in a round-robin way to balance load. Consumers read messages from partitions to process them. This flow helps Kafka handle many messages efficiently and keep order per key.