Challenge - 5 Problems
Kafka Consumer Mastery
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 consumer code snippet?
Consider a Kafka consumer configured to read messages from a topic. What will be printed when the consumer processes messages?
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer('test-topic', bootstrap_servers=['localhost:9092']) for message in consumer: print(f"Received: {message.value.decode('utf-8')}") break
Attempts:
2 left
💡 Hint
Remember that Kafka messages are bytes and need decoding.
✗ Incorrect
KafkaConsumer returns messages as bytes. Decoding with utf-8 converts bytes to string, so the output is the string message content.
🧠 Conceptual
intermediate1:30remaining
Why do Kafka consumers commit offsets after processing messages?
Choose the main reason why Kafka consumers commit offsets after processing messages.
Attempts:
2 left
💡 Hint
Think about how consumers know where to continue after a restart.
✗ Incorrect
Committing offsets tells Kafka which messages the consumer has processed. This prevents processing the same message multiple times after a restart.
🔧 Debug
advanced2:00remaining
What error will this Kafka consumer code raise?
Identify the error raised by this Kafka consumer code snippet.
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer('topic1', bootstrap_servers=['localhost:9092']) for message in consumer: print(message.value.upper())
Attempts:
2 left
💡 Hint
Remember the type of message.value in KafkaConsumer.
✗ Incorrect
Kafka messages are bytes. Calling upper() directly on bytes raises AttributeError because bytes do not have upper() method.
🚀 Application
advanced2:30remaining
How to ensure exactly-once processing in Kafka consumers?
Which approach helps Kafka consumers achieve exactly-once processing of messages?
Attempts:
2 left
💡 Hint
Think about avoiding duplicates and message loss.
✗ Incorrect
Idempotent producers avoid duplicate writes. Committing offsets after processing ensures messages are processed once and offsets reflect that.
❓ Predict Output
expert3:00remaining
What is the output of this Kafka consumer batch processing code?
Given this Kafka consumer code that processes messages in batches, what will be the output?
Kafka
from kafka import KafkaConsumer consumer = KafkaConsumer('batch-topic', bootstrap_servers=['localhost:9092'], max_poll_records=3) batch = [] for message in consumer: batch.append(message.value.decode('utf-8')) if len(batch) == 3: print(batch) batch.clear() break
Attempts:
2 left
💡 Hint
max_poll_records controls how many messages are fetched per poll.
✗ Incorrect
The consumer fetches up to 3 messages per poll. The code collects 3 messages, prints them as a list, then breaks the loop.