Challenge - 5 Problems
Kafka Offset Master
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 offset commit code?
Consider the following Kafka consumer code snippet that commits offsets synchronously. What will be printed if the commit succeeds?
Kafka
consumer.subscribe(['my_topic']) try: records = consumer.poll(timeout_ms=1000) for record in records: print(f"Received: {record.value}") consumer.commit() print("Offsets committed successfully") except Exception as e: print(f"Commit failed: {e}")
Attempts:
2 left
💡 Hint
The commit() call happens after messages are received and printed.
✗ Incorrect
The consumer polls messages and prints each received message's value. After processing, commit() commits the offsets synchronously. If successful, the message 'Offsets committed successfully' is printed. So the output includes the received message(s) followed by the commit success message.
🧠 Conceptual
intermediate1:30remaining
Which offset reset policy causes the consumer to start from the earliest offset when no committed offset exists?
Kafka consumers can be configured with an offset reset policy. Which of the following values for 'auto.offset.reset' causes the consumer to read from the earliest offset if no committed offset is found?
Attempts:
2 left
💡 Hint
Think about starting from the beginning of the topic.
✗ Incorrect
Setting 'auto.offset.reset' to 'earliest' tells the consumer to start reading from the earliest available offset if no committed offset exists. 'latest' starts from the end, 'none' throws an error, and 'auto' is not a valid value.
❓ Predict Output
advanced2:00remaining
What error does this Kafka consumer code raise?
Given the following code snippet, what error will be raised when running it?
Kafka
consumer.assign([TopicPartition('my_topic', 0)]) consumer.seek(TopicPartition('my_topic', 0), 100) records = consumer.poll(timeout_ms=1000) for record in records: print(record.value)
Attempts:
2 left
💡 Hint
Assigning partitions manually allows seeking to offsets.
✗ Incorrect
Using assign() manually assigns partitions, so the consumer is not subscribed but can seek offsets. If offset 100 exists, poll returns messages from there without error. No subscription error occurs because assign() is used instead of subscribe().
❓ Predict Output
advanced2:00remaining
What is the output of this asynchronous offset commit example?
Examine this Kafka consumer code that commits offsets asynchronously with a callback. What will be printed if the commit fails?
Kafka
def on_commit(err, partitions): if err: print(f"Commit failed for partitions: {partitions}") else: print("Commit succeeded") consumer.subscribe(['my_topic']) consumer.poll(timeout_ms=1000) consumer.commit_async(callback=on_commit)
Attempts:
2 left
💡 Hint
The callback prints a message only if there is an error.
✗ Incorrect
If the commit fails, the callback receives an error and prints the failure message with the partitions. If it succeeds, it prints 'Commit succeeded'. The question asks about the failure case, so the failure message is printed.
🧠 Conceptual
expert2:30remaining
How many committed offsets are stored if a consumer group consumes 3 partitions and commits offsets only for 2 partitions?
A Kafka consumer group reads from 3 partitions of a topic. It commits offsets only for 2 partitions and never commits for the third. How many committed offsets will Kafka store for this group?
Attempts:
2 left
💡 Hint
Offsets are stored per partition independently.
✗ Incorrect
Kafka stores committed offsets per partition per consumer group. If offsets are committed only for 2 partitions, only those 2 offsets are stored. The third partition has no committed offset until committed.