0
0
Kafkadevops~20 mins

Subscribing to topics in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Subscription Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AAn empty list [] because the consumer does not subscribe to any topic
BA list of all messages from the beginning of 'my_topic' if any exist, e.g., ['msg1', 'msg2']
CA runtime error because 'auto_offset_reset' is not a valid parameter
DA list containing only the latest message from 'my_topic'
Attempts:
2 left
💡 Hint
Check how 'auto_offset_reset' affects the starting point of message consumption.
🧠 Conceptual
intermediate
1:30remaining
Understanding Kafka Consumer Group Subscription
In Kafka, what happens when multiple consumers in the same group subscribe to the same topic?
AMessages are load balanced so each message is delivered to only one consumer in the group.
BEach consumer receives all messages from the topic independently.
COnly the first consumer receives messages; others remain idle.
DConsumers receive messages only if they subscribe to different topics.
Attempts:
2 left
💡 Hint
Think about how Kafka distributes partitions among consumers in a group.
🔧 Debug
advanced
2: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'))
AThe topic name is incorrect; it should be 'my-topic' with a dash.
BThe consumer forgot to call 'poll()' before iterating over messages.
CThe consumer must specify 'enable_auto_commit=False' to receive messages.
DThe consumer does not specify 'auto_offset_reset', so it starts at the latest offset and misses existing messages.
Attempts:
2 left
💡 Hint
Check the default behavior of 'auto_offset_reset' when no committed offset exists.
📝 Syntax
advanced
1: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'])
Aconsumer.subscribe(['topic1', 'topic2'])
Bconsumer.subscribe(topic=['topic1', 'topic2'])
Cconsumer.subscribe('topic1', 'topic2')
Dconsumer.subscribe(topics=['topic1', 'topic2'])
Attempts:
2 left
💡 Hint
Check the method signature for 'subscribe' in KafkaConsumer.
🚀 Application
expert
2: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?
AEach consumer will be assigned 2 partitions.
BEach consumer will be assigned all 6 partitions.
COne consumer gets 4 partitions, others get 1 each.
DPartitions are assigned randomly, so counts vary each time.
Attempts:
2 left
💡 Hint
Kafka tries to balance partitions evenly among consumers in a group.