0
0
Kafkadevops~10 mins

Partitioner behavior in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Partitioner behavior
Producer sends message
Check if key is provided
Compute hash of key
Calculate partition number
Send message to chosen partition
The producer decides the partition for each message by checking if a key exists. If yes, it hashes the key to pick a partition; if no, it uses round-robin or default logic.
Execution Sample
Kafka
key = "user123"
num_partitions = 3
partition = hash(key) % num_partitions
send_to_partition(partition)
This code picks a partition for a message with key 'user123' by hashing the key and using modulo with the number of partitions.
Process Table
StepActionInputComputationResult
1Producer prepares messagekey='user123'N/AMessage ready with key
2Check if key existskey='user123'key existsProceed to hash key
3Compute hashkey='user123'hash('user123')e.g. 123456789
4Calculate partitionhash=123456789, partitions=3123456789 % 30
5Send messagepartition=0N/AMessage sent to partition 0
6Next message without keykey=NoneNo keyUse round-robin partitioner
7Round-robin selects partitionpartitions=3next partition in sequencepartition 1
8Send messagepartition=1N/AMessage sent to partition 1
9ExitNo more messagesN/AProducer stops sending
💡 No more messages to send, producer stops execution
Status Tracker
VariableStartAfter Step 3After Step 4After Step 7Final
keyNone"user123""user123"NoneNone
hash(key)N/A123456789123456789N/AN/A
partitionN/AN/A011
Key Moments - 3 Insights
Why does the partition number depend on the key's hash?
Because hashing the key ensures messages with the same key always go to the same partition, as shown in step 3 and 4 of the execution_table.
What happens if the message has no key?
The producer uses a round-robin or default partitioner to distribute messages evenly, as seen in steps 6 and 7.
Can the partition number be greater than the number of partitions?
No, because the modulo operation (step 4) limits the partition number to between 0 and num_partitions-1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the partition number calculated for key 'user123'?
A1
B2
C0
D3
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does the producer decide to use round-robin partitioning?
AStep 6
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where key is None and round-robin is chosen in the execution_table.
If the number of partitions changes from 3 to 4, how does it affect the partition calculation?
APartition number stays 0 to 2
BPartition number can now be 0 to 3
CHash value changes
DProducer stops sending messages
💡 Hint
Refer to the modulo operation in step 4 and how it depends on num_partitions.
Concept Snapshot
Partitioner behavior in Kafka:
- If message has a key, hash the key.
- Use modulo with number of partitions to pick partition.
- If no key, use round-robin or default partitioner.
- Ensures messages with same key go to same partition.
- Partition number always between 0 and num_partitions-1.
Full Transcript
This visual execution shows how Kafka's producer decides which partition to send a message to. First, it checks if the message has a key. If yes, it computes a hash of the key and uses modulo with the number of partitions to find the partition number. This ensures messages with the same key always go to the same partition. If there is no key, the producer uses a round-robin or default partitioner to distribute messages evenly across partitions. The partition number is always between zero and one less than the total partitions. The execution table traces these steps with example values, and the variable tracker shows how key, hash, and partition variables change. Key moments clarify common confusions about hashing, no-key messages, and partition number limits. The quiz tests understanding of partition calculation and behavior changes when partitions count changes.