Bird
Raised Fist0

Identify the bug in this Kafka consumer code that aims for at-least-once delivery:

medium📝 Debug Q14 of Q15
Kafka - Message Delivery Semantics
Identify the bug in this Kafka consumer code that aims for at-least-once delivery:
consumer.subscribe(['topic'])
for message in consumer:
    consumer.commit()
    process(message)
AUsing subscribe instead of assign causes duplicates
BCommitting offset before processing causes message loss on failure
CNot committing offsets causes infinite reprocessing
Dprocess() function is missing a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Check commit position relative to processing

    Offsets are committed before processing, so if processing fails, message is lost.
  2. Step 2: Understand at-least-once requirement

    To guarantee at-least-once, commit must happen after successful processing to avoid loss.
  3. Final Answer:

    Committing offset before processing causes message loss on failure -> Option B
  4. Quick Check:

    Commit after processing to avoid loss [OK]
Quick Trick: Commit offsets only after processing completes [OK]
Common Mistakes:
MISTAKES
  • Committing before processing causes data loss
  • Ignoring commit timing importance
  • Confusing subscribe with assign usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes