Bird
Raised Fist0

Examine the following Kafka consumer code snippet. What is the primary issue that could cause the consumer to miss messages?

medium📝 Debug Q7 of Q15
Kafka - with Java/Python

Examine the following Kafka consumer code snippet. What is the primary issue that could cause the consumer to miss messages?

consumer.subscribe(Collections.singletonList("topic1"));
while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println(record.value());
    }
}
AThe poll timeout is too short, causing the consumer to miss messages.
BThe consumer does not commit offsets, so it may reprocess messages after restart.
CThe consumer is subscribed to only one topic, which is invalid for multiple partitions.
DThe consumer should use poll(Duration.ofSeconds(10)) instead of milliseconds.
Step-by-Step Solution
Solution:
  1. Step 1: Identify offset management

    The code does not call commitSync() or commitAsync() to commit offsets after processing messages.
  2. Step 2: Understand consequences

    Without committing offsets, if the consumer restarts, it will re-read the same messages, causing duplicates.
  3. Final Answer:

    The consumer does not commit offsets, so it may reprocess messages after restart. -> Option B
  4. Quick Check:

    Offset commits are essential to avoid duplicate processing. [OK]
Quick Trick: Always commit offsets after processing messages [OK]
Common Mistakes:
MISTAKES
  • Assuming poll timeout affects message delivery
  • Believing subscribing to one topic is invalid
  • Thinking poll duration must be in seconds

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes