Partitioner behavior in Kafka - Deep Dive
Kafka Topic ┌───────────────┐ │ Partition 0 │ │ ┌───────────┐ │ │ │ Messages │ │ │ └───────────┘ │ ├───────────────┤ │ Partition 1 │ │ ┌───────────┐ │ │ │ Messages │ │ │ └───────────┘ │ ├───────────────┤ │ Partition 2 │ │ ┌───────────┐ │ │ │ Messages │ │ │ └───────────┘ │ └───────────────┘ Producer --(partitioner)--> Chooses partition based on key or logic
Producer Client ┌─────────────────────┐ │ Message with Key? │ │ ┌───────────────┐ │ │ │ Yes │───┼──> Hash(key) % partitions -> Partition N │ └───────────────┘ │ │ ┌───────────────┐ │ │ │ No │───┼──> Round-robin -> Partition M │ └───────────────┘ │ └─────────────────────┘ Partition N -> Broker X Partition M -> Broker Y
Practice
What does a Kafka partitioner do?
partitioner decides:
Solution
Step 1: Understand the role of a partitioner
A partitioner decides the partition inside a Kafka topic where a message will be stored.Step 2: Match the function to the options
Only Which partition a message is sent to correctly describes this role.Final Answer:
Which partition a message is sent to -> Option AQuick Check:
Partitioner = decides message partition [OK]
- Confusing partitioner with compression
- Thinking partitioner sets topic name
- Assuming partitioner changes message format
Which of the following is the correct way to specify a default partitioner class in Kafka producer configuration?
props.put("partitioner.class", ?);Solution
Step 1: Recall default partitioner class name
The default partitioner class in Kafka isDefaultPartitionerlocated inorg.apache.kafka.clients.producer.internals.Step 2: Match the correct class name
"org.apache.kafka.clients.producer.internals.DefaultPartitioner" matches the default partitioner class name exactly.Final Answer:
"org.apache.kafka.clients.producer.internals.DefaultPartitioner" -> Option DQuick Check:
Default partitioner class = DefaultPartitioner [OK]
- Using non-existent class names
- Confusing round-robin with default partitioner
- Misspelling package or class names
Given a Kafka topic with 3 partitions and a producer sending messages with keys "key1", "key2", and "key1" again, which partitions will the messages go to if the default partitioner is used?
Solution
Step 1: Understand key-based partitioning
Messages with the same key go to the same partition. Different keys may go to different partitions.Step 2: Assign partitions for keys
Assuming hash(key) mod 3,"key1"maps to partition 0,"key2"maps to partition 1. So messages go to partitions [0, 1, 0].Final Answer:
[0, 1, 0] -> Option CQuick Check:
Same key = same partition [OK]
- Assuming keys distribute randomly
- Assigning different partitions to same key
- Ignoring modulo partition count
Consider this code snippet for sending messages without keys:
for i in range(4):
producer.send("topic", value=f"msg{i}")What is the likely issue if all messages end up in the same partition?
Solution
Step 1: Analyze message keys and partition count
Messages have no keys, so default partitioner uses round-robin if multiple partitions exist.Step 2: Check topic partition count
If all messages go to the same partition, likely the topic has only one partition.Final Answer:
Topic has only one partition -> Option BQuick Check:
Single partition topic = all messages same partition [OK]
- Assuming round-robin works with one partition
- Thinking keys are present when they are not
- Ignoring topic partition count
You want to ensure messages with the same user ID always go to the same partition, but messages without user ID keys should be evenly distributed. Which partitioner behavior should you rely on?
Solution
Step 1: Understand keyed message behavior
Default partitioner sends messages with the same key to the same partition.Step 2: Understand unkeyed message behavior
Messages without keys are distributed evenly using round-robin by default partitioner.Step 3: Match behavior to requirement
Default partitioner: keyed messages to same partition, unkeyed round-robin matches the requirement exactly.Final Answer:
Default partitioner: keyed messages to same partition, unkeyed round-robin -> Option AQuick Check:
Default partitioner fits keyed + unkeyed needs [OK]
- Choosing custom partitioner ignoring keys
- Using round-robin for keyed messages
- Sending all messages to one partition
Ready to prove it?
Pass the test and this pattern moves unknown
