Bird
0
0

What is wrong with the following code snippet for committing offsets manually?

medium📝 Debug Q14 of 15
Kafka - Consumers
What is wrong with the following code snippet for committing offsets manually?
consumer.poll(Duration.ofMillis(1000));
consumer.commitAsync();
consumer.close();
Aconsumer.close() should be called before commitAsync()
BcommitAsync() should be called before poll()
CcommitAsync() is called without processing polled records
Dpoll() duration is too short for commitAsync()
Step-by-Step Solution
Solution:
  1. Step 1: Check offset commit timing

    Offsets should be committed after processing records, not just after poll().
  2. Step 2: Identify missing processing step

    The code commits offsets without handling any messages, so it commits offsets that may not be processed.
  3. Final Answer:

    commitAsync() is called without processing polled records -> Option C
  4. Quick Check:

    Commit after processing, not just after poll [OK]
Quick Trick: Always process records before committing offsets [OK]
Common Mistakes:
  • Calling commit before processing messages
  • Closing consumer before committing
  • Misunderstanding poll duration effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes