Bird
Raised Fist0

Identify the error in this Kafka consumer code snippet: ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord record : records) { process(record); } consumer.commitSync();

medium📝 Debug Q6 of Q15
Kafka - with Java/Python
Identify the error in this Kafka consumer code snippet: ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord record : records) { process(record); } consumer.commitSync();
ANo error; code handles errors implicitly
Bprocess() must be inside a try-catch block
CcommitSync() should be called before processing records
Dpoll() may throw exceptions that are not caught
Step-by-Step Solution
Solution:
  1. Step 1: Review poll() method behavior

    poll() can throw exceptions like TimeoutException or WakeupException.
  2. Step 2: Check error handling in code

    Code does not catch exceptions from poll(), so unhandled exceptions may crash the consumer.
  3. Final Answer:

    poll() may throw exceptions that are not caught -> Option D
  4. Quick Check:

    poll exceptions must be caught to avoid crashes [OK]
Quick Trick: Always catch exceptions from poll() in consumers [OK]
Common Mistakes:
MISTAKES
  • Assuming poll() never throws exceptions
  • Thinking commitSync() order causes errors
  • Believing process() must always have try-catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes