Bird
Raised Fist0

Identify the error in this Kafka consumer code for at-least-once delivery:

medium📝 Debug Q6 of Q15
Kafka - Message Delivery Semantics
Identify the error in this Kafka consumer code for at-least-once delivery:
consumer.subscribe(['topic'])
for msg in consumer:
    process(msg)
consumer.commit()
Aprocess() should be called after commit()
Bsubscribe() should be called inside the loop
Ccommit() is called outside the loop, so offsets are not committed per message
Dcommit() requires a parameter specifying the offset
Step-by-Step Solution
Solution:
  1. Step 1: Review commit placement

    commit() is outside the loop, so offsets are committed only once after all messages, risking message loss on failure.
  2. Step 2: Correct commit usage

    For at-least-once, commit should be called after processing each message inside the loop to ensure offsets are saved timely.
  3. Final Answer:

    commit() is called outside the loop, so offsets are not committed per message -> Option C
  4. Quick Check:

    Commit must be inside loop for at-least-once [OK]
Quick Trick: Call commit() inside loop after processing each message [OK]
Common Mistakes:
MISTAKES
  • Calling commit() only once outside loop
  • Misplacing subscribe() inside loop
  • Assuming commit() needs parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes