Challenge - 5 Problems
Kafka Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the partition chosen for this Kafka message?
Given a Kafka topic with 3 partitions and the following producer code, what partition will the message be sent to?
Kafka
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') # Key is a string 'user42' key = b'user42' value = b'Hello Kafka' # Send message future = producer.send('my_topic', key=key, value=value) record_metadata = future.get(timeout=10) print(record_metadata.partition)
Attempts:
2 left
💡 Hint
Kafka uses the hash of the key modulo the number of partitions to decide the partition.
✗ Incorrect
Kafka hashes the key 'user42' and then takes modulo 3 (number of partitions). The hash of 'user42' modulo 3 results in partition 1.
🧠 Conceptual
intermediate1:30remaining
Why is using a partition key important in Kafka?
Select the main reason why using a partition key is important when producing messages to Kafka.
Attempts:
2 left
💡 Hint
Think about message ordering guarantees in Kafka.
✗ Incorrect
Kafka guarantees order only within a partition. Using a key ensures all messages with that key go to the same partition, preserving order.
🔧 Debug
advanced2:00remaining
Why does this Kafka producer code cause uneven message distribution?
This Kafka producer sends messages with keys that are always None. What is the likely effect on partition distribution?
Kafka
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') for i in range(10): producer.send('my_topic', key=None, value=f'Message {i}'.encode()) producer.flush()
Attempts:
2 left
💡 Hint
What happens when no key is provided in Kafka producer?
✗ Incorrect
When key is None, Kafka uses a round-robin or default partitioner which may send all messages to the same partition if the producer is single-threaded or due to batching.
📝 Syntax
advanced1:30remaining
Which Kafka producer code snippet correctly sets a partition key?
Choose the code snippet that correctly sets a string key for Kafka message production.
Attempts:
2 left
💡 Hint
Kafka keys must be bytes, not strings or integers.
✗ Incorrect
Kafka producer expects the key as bytes. Passing a string without encoding causes an error. Passing an integer is invalid. None means no key.
🚀 Application
expert2:30remaining
How many partitions will contain messages after this code runs?
A Kafka topic has 4 partitions. The producer sends messages with keys cycling through ['A', 'B', 'C']. How many partitions will have messages after sending 12 messages?
Kafka
keys = [b'A', b'B', b'C'] for i in range(12): key = keys[i % 3] producer.send('my_topic', key=key, value=b'data') producer.flush()
Attempts:
2 left
💡 Hint
Messages with the same key always go to the same partition.
✗ Incorrect
There are 3 distinct keys cycling, so messages go to 3 partitions. The 4th partition gets no messages.