Challenge - 5 Problems
Kafka Subscription Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Kafka Consumer Subscription Behavior
What will be the output of this Kafka consumer code snippet when it subscribes to a topic and polls for records?
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer( 'my_topic', bootstrap_servers=['localhost:9092'], auto_offset_reset='earliest', consumer_timeout_ms=1000 ) messages = [] for message in consumer: messages.append(message.value.decode('utf-8')) print(messages)
Attempts:
2 left
💡 Hint
Check how 'auto_offset_reset' affects the starting point of message consumption.
✗ Incorrect
The consumer subscribes to 'my_topic' and starts reading from the earliest offset due to 'auto_offset_reset="earliest"'. It collects all messages available during the poll and prints them as a list.
🧠 Conceptual
intermediate1:30remaining
Understanding Kafka Consumer Group Subscription
In Kafka, what happens when multiple consumers in the same group subscribe to the same topic?
Attempts:
2 left
💡 Hint
Think about how Kafka distributes partitions among consumers in a group.
✗ Incorrect
Kafka divides topic partitions among consumers in the same group so each message is processed by only one consumer, enabling parallel processing without duplication.
🔧 Debug
advanced2:30remaining
Why does this Kafka consumer not receive messages?
Given this Kafka consumer code, why does it not receive any messages even though the topic has data?
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer( bootstrap_servers=['localhost:9092'], group_id='group1' ) consumer.subscribe(['my_topic']) for message in consumer: print(message.value.decode('utf-8'))
Attempts:
2 left
💡 Hint
Check the default behavior of 'auto_offset_reset' when no committed offset exists.
✗ Incorrect
Without 'auto_offset_reset' set to 'earliest', the consumer starts reading from the latest offset, so it misses all existing messages in the topic.
📝 Syntax
advanced1:30remaining
Identify the syntax error in Kafka consumer subscription code
Which option contains a syntax error in subscribing a Kafka consumer to multiple topics?
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer(bootstrap_servers=['localhost:9092']) consumer.subscribe(topics=['topic1', 'topic2'])
Attempts:
2 left
💡 Hint
Check the method signature for 'subscribe' in KafkaConsumer.
✗ Incorrect
'subscribe' expects a single argument: a list of topic names or a pattern. Passing multiple string arguments without a list causes a syntax error.
🚀 Application
expert2:00remaining
Determine the number of partitions assigned to each consumer
You have a Kafka topic with 6 partitions and 3 consumers in the same group subscribing to it. How many partitions will each consumer be assigned?
Attempts:
2 left
💡 Hint
Kafka tries to balance partitions evenly among consumers in a group.
✗ Incorrect
Kafka assigns partitions evenly among consumers in a group. With 6 partitions and 3 consumers, each gets 2 partitions.