Bird
0
0

Given the following Kafka consumer code snippet with auto-commit enabled:

medium📝 Predict Output Q4 of 15
Kafka - Consumers
Given the following Kafka consumer code snippet with auto-commit enabled:
Properties props = new Properties();
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
KafkaConsumer consumer = new KafkaConsumer<>(props);
consumer.subscribe(List.of("topic1"));
ConsumerRecords records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord record : records) {
    System.out.println(record.value());
}

What happens if the consumer crashes immediately after this code runs?
AOffsets are committed automatically, so no duplicate processing occurs
BOffsets are not committed, so messages may be reprocessed
CConsumer throws an exception due to missing manual commit
DConsumer stops consuming messages permanently
Step-by-Step Solution
Solution:
  1. Step 1: Understand auto-commit timing

    Auto-commit commits offsets at intervals (here every 1000 ms), not immediately after poll.
  2. Step 2: Analyze crash timing effect

    If the consumer crashes before the next auto-commit, offsets are not committed, causing possible reprocessing.
  3. Final Answer:

    Offsets are not committed, so messages may be reprocessed -> Option B
  4. Quick Check:

    Crash before auto-commit interval = possible duplicate processing [OK]
Quick Trick: Auto-commit interval delays offset commit; crash may cause duplicates [OK]
Common Mistakes:
  • Assuming offsets commit immediately after poll
  • Confusing auto-commit with manual commit
  • Believing consumer crashes stop message consumption forever

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes