Challenge - 5 Problems
Kafka Partitioner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the partition chosen for a given key?
Given a Kafka producer configured with 3 partitions and the default partitioner, what partition will the message with key
"user123" be sent to?Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String, String> producer = new KafkaProducer<>(props); String topic = "test-topic"; String key = "user123"; int numPartitions = 3; // Assume default partitioner uses key.hashCode() % numPartitions int partition = Math.abs(key.hashCode()) % numPartitions; System.out.println(partition);
Attempts:
2 left
💡 Hint
Remember the default partitioner uses the hash code of the key modulo the number of partitions.
✗ Incorrect
The default Kafka partitioner calculates the partition by taking the absolute value of the key's hash code modulo the number of partitions. For key "user123" and 3 partitions, the calculation results in partition 1.
❓ Predict Output
intermediate2:00remaining
What happens if the key is null?
In Kafka's default partitioner, what partition will a message be sent to if the key is
null and the topic has 4 partitions?Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String, String> producer = new KafkaProducer<>(props); String topic = "test-topic"; String key = null; int numPartitions = 4; // Default partitioner behavior for null key int partition = new Random().nextInt(numPartitions); System.out.println(partition);
Attempts:
2 left
💡 Hint
Check how the default partitioner handles null keys.
✗ Incorrect
When the key is null, Kafka's default partitioner assigns the message to a random partition among the available partitions.
🧠 Conceptual
advanced1:30remaining
Why use a custom partitioner?
Which of the following is the main reason to implement a custom partitioner in Kafka?
Attempts:
2 left
💡 Hint
Think about what partitioning controls in Kafka.
✗ Incorrect
A custom partitioner lets you decide which partition a message goes to based on your own rules, helping with load balancing or message ordering.
❓ Predict Output
advanced1:30remaining
What error occurs with invalid partition number?
What error will Kafka producer throw if you try to send a message to partition 5 on a topic with only 4 partitions (0 to 3)?
Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", 5, "key", "value"); producer.send(record);
Attempts:
2 left
💡 Hint
Check Kafka's validation for partition numbers.
✗ Incorrect
Kafka producer validates the partition number and throws IllegalArgumentException if it is outside the valid range.
🧠 Conceptual
expert2:00remaining
How does Kafka ensure message order within a partition?
Which statement best explains how Kafka guarantees message order within a partition?
Attempts:
2 left
💡 Hint
Think about how keys and partitions relate to ordering.
✗ Incorrect
Kafka guarantees order only within a partition. By sending messages with the same key to the same partition, order is preserved for that key.