Consider a Kafka consumer configured for at-least-once delivery. What will be the output of the following code snippet when consuming messages?
from kafka import KafkaConsumer consumer = KafkaConsumer('my_topic', group_id='group1', enable_auto_commit=False) for message in consumer: print(f"Received: {message.value.decode('utf-8')}") consumer.commit() break
Think about what happens when auto commit is disabled and commit() is called manually after processing a message.
Since auto commit is disabled, the consumer must commit offsets manually. The code commits after processing the first message, so it prints it once and commits the offset, ensuring at-least-once delivery.
Which configuration option in Kafka consumer guarantees at-least-once message delivery?
At-least-once means messages can be processed more than once but never lost.
Disabling auto commit and committing offsets manually after processing ensures that messages are not lost but might be processed multiple times, which is at-least-once delivery.
Given the following code, why might some messages be lost even though manual commits are used?
consumer = KafkaConsumer('topic', group_id='g1', enable_auto_commit=False) for msg in consumer: process(msg) if some_condition(): consumer.commit()
Think about when offsets are committed in relation to message processing.
Since commits happen only if some_condition() is true, messages processed when the condition is false are not committed, causing potential message loss on restart.
Choose the correct code snippet that commits offsets manually after processing each message.
consumer = KafkaConsumer('topic', group_id='g1', enable_auto_commit=False) for msg in consumer: process(msg) # commit offset here
consumer.commit() without arguments is the standard way to manually commit offsets after processing a message.
Option A calls consumer.commit(), which automatically commits the tracked offsets up to the next offset after the last processed message. This is the correct and standard way for manual commits in kafka-python.
You want to ensure at-least-once delivery in a Kafka system. Which combination of producer and consumer settings and code logic achieves this?
At-least-once means messages are never lost but may be duplicated.
Producer with acks='all' and retries ensures messages are not lost on send. Consumer disabling auto commit and committing offsets after processing ensures messages are processed at least once.