Challenge - 5 Problems
Kafka Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kafka partition assignment code?
Consider a Kafka consumer group with 3 consumers and a topic with 6 partitions. The partitions are assigned evenly. What partitions will consumer 2 receive?
Kafka
consumers = ['consumer1', 'consumer2', 'consumer3'] partitions = list(range(6)) assignments = {c: [] for c in consumers} for i, p in enumerate(partitions): assignments[consumers[i % len(consumers)]].append(p) print(assignments['consumer2'])
Attempts:
2 left
💡 Hint
Partitions are assigned in a round-robin fashion based on the consumer index.
✗ Incorrect
Partitions 0, 3 go to consumer1; 1, 4 go to consumer2; 2, 5 go to consumer3. So consumer2 gets partitions 1 and 4.
🧠 Conceptual
intermediate1:30remaining
How does Kafka decide which partition a message goes to when a key is provided?
In Kafka, when producing a message with a key, how is the partition chosen?
Attempts:
2 left
💡 Hint
Think about how consistent partitioning is achieved for messages with the same key.
✗ Incorrect
Kafka uses a hash function on the key and then takes modulo with the number of partitions to ensure messages with the same key go to the same partition.
❓ Predict Output
advanced2:00remaining
What error does this Kafka producer code raise?
This code tries to send a message to a non-existent partition. What error will it raise?
Kafka
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') producer.send('my_topic', key=b'key1', value=b'value1', partition=10) producer.flush()
Attempts:
2 left
💡 Hint
Check what happens if you specify a partition index that does not exist in the topic.
✗ Incorrect
Kafka raises UnknownTopicOrPartitionError when the specified partition does not exist for the topic.
🚀 Application
advanced1:30remaining
How many partitions will be assigned to each consumer in this group?
A Kafka topic has 7 partitions. There are 4 consumers in the same group consuming this topic. How many partitions will each consumer get?
Attempts:
2 left
💡 Hint
Partitions are distributed as evenly as possible among consumers.
✗ Incorrect
7 partitions divided among 4 consumers means 3 consumers get 2 partitions, and 1 consumer gets 1 partition.
🧠 Conceptual
expert2:00remaining
What is the impact of increasing the number of partitions on message ordering in Kafka?
If you increase the number of partitions for a Kafka topic, what happens to the ordering guarantees of messages with the same key?
Attempts:
2 left
💡 Hint
Think about how Kafka uses keys to assign partitions and maintain order.
✗ Incorrect
Kafka guarantees ordering per partition. Since keys map to partitions via hashing, messages with the same key stay in the same partition even if partitions increase.