0
0
Kafkadevops~20 mins

Why consumers process messages in Kafka - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Consumer Mastery
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 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
AReceived: Hello World
BReceived: b'Hello World'
CReceived: None
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that Kafka messages are bytes and need decoding.
🧠 Conceptual
intermediate
1:30remaining
Why do Kafka consumers commit offsets after processing messages?
Choose the main reason why Kafka consumers commit offsets after processing messages.
ATo encrypt messages for security
BTo increase the speed of message consumption
CTo delete messages from the Kafka topic
DTo track which messages have been processed and avoid reprocessing
Attempts:
2 left
💡 Hint
Think about how consumers know where to continue after a restart.
🔧 Debug
advanced
2: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())
AAttributeError: 'bytes' object has no attribute 'upper'
BNameError: name 'message' is not defined
CNo error, prints uppercase message
DTypeError: unsupported operand type(s) for +: 'int' and 'str'
Attempts:
2 left
💡 Hint
Remember the type of message.value in KafkaConsumer.
🚀 Application
advanced
2:30remaining
How to ensure exactly-once processing in Kafka consumers?
Which approach helps Kafka consumers achieve exactly-once processing of messages?
AUse multiple consumers in the same group without coordination
BUse idempotent producers and commit offsets after processing
CCommit offsets before processing messages
DConsume messages without committing offsets
Attempts:
2 left
💡 Hint
Think about avoiding duplicates and message loss.
Predict Output
expert
3: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
A['msg1', 'msg2']
B['msg1']
C['msg1', 'msg2', 'msg3']
DSyntaxError
Attempts:
2 left
💡 Hint
max_poll_records controls how many messages are fetched per poll.