0
0
Kafkadevops~20 mins

Partition key and routing in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
APartition 0
BPartition 3
CPartition 2
DPartition 1
Attempts:
2 left
💡 Hint
Kafka uses the hash of the key modulo the number of partitions to decide the partition.
🧠 Conceptual
intermediate
1: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.
AIt ensures messages with the same key go to the same partition, preserving order.
BIt encrypts the message content for security.
CIt compresses the message to save bandwidth.
DIt automatically retries sending messages on failure.
Attempts:
2 left
💡 Hint
Think about message ordering guarantees in Kafka.
🔧 Debug
advanced
2: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()
AMessages are evenly distributed across all partitions.
BAll messages go to the same partition, causing uneven load.
CMessages cause a runtime error because key cannot be None.
DMessages are dropped silently without sending.
Attempts:
2 left
💡 Hint
What happens when no key is provided in Kafka producer?
📝 Syntax
advanced
1: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.
Aproducer.send('topic', key='user1', value=b'data')
Bproducer.send('topic', key=123, value=b'data')
Cproducer.send('topic', key=b'user1', value=b'data')
Dproducer.send('topic', key=None, value=b'data')
Attempts:
2 left
💡 Hint
Kafka keys must be bytes, not strings or integers.
🚀 Application
expert
2: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()
A3 partitions
B4 partitions
C1 partition
D0 partitions
Attempts:
2 left
💡 Hint
Messages with the same key always go to the same partition.