0
0
Kafkadevops~20 mins

Partition concept in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'])
A[0, 3]
B[2, 5]
C[1, 3, 5]
D[1, 4]
Attempts:
2 left
💡 Hint
Partitions are assigned in a round-robin fashion based on the consumer index.
🧠 Conceptual
intermediate
1: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?
AKafka hashes the key and assigns the message to the partition based on the hash modulo the number of partitions.
BKafka always sends messages with keys to partition 0.
CKafka randomly assigns the message to any partition regardless of the key.
DKafka assigns messages with keys to partitions in a round-robin manner.
Attempts:
2 left
💡 Hint
Think about how consistent partitioning is achieved for messages with the same key.
Predict Output
advanced
2: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()
Akafka.errors.UnknownTopicOrPartitionError
Bkafka.errors.KafkaTimeoutError
CValueError: Partition index out of range
DNo error, message sent successfully
Attempts:
2 left
💡 Hint
Check what happens if you specify a partition index that does not exist in the topic.
🚀 Application
advanced
1: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?
AConsumers 1 to 3 get 1 partition each, consumer 4 gets 4 partitions
BConsumers 1 to 3 get 2 partitions each, consumer 4 gets 1 partition
CEach consumer gets 2 partitions, and 1 partition is assigned to a random consumer
DEach consumer gets exactly 1 partition, and 3 partitions remain unassigned
Attempts:
2 left
💡 Hint
Partitions are distributed as evenly as possible among consumers.
🧠 Conceptual
expert
2: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?
AOrdering is preserved globally across all partitions regardless of key.
BOrdering is lost because messages with the same key can be sent to different partitions after increasing partitions.
COrdering is preserved per key because messages with the same key always go to the same partition.
DOrdering is preserved only if the number of partitions is a power of two.
Attempts:
2 left
💡 Hint
Think about how Kafka uses keys to assign partitions and maintain order.