0
0
Kafkadevops~20 mins

Auto-commit vs manual commit in Kafka - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Commit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when auto-commit is enabled?

Consider a Kafka consumer configured with enable.auto.commit=true and auto.commit.interval.ms=1000. The consumer reads messages continuously. What happens if the consumer crashes after processing some messages but before the next auto-commit?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));

while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
        // Process message
    }
    // No manual commit here
}
AThe consumer will throw an exception if it crashes before committing offsets.
BOffsets are committed manually by the user after processing each message.
CAll processed messages are guaranteed committed immediately after processing each message.
DMessages processed before the crash may be reprocessed after restart because the offset was not committed yet.
Attempts:
2 left
💡 Hint

Think about how auto-commit interval affects when offsets are saved.

Predict Output
intermediate
2:00remaining
What happens when manual commit is used incorrectly?

A Kafka consumer is configured with enable.auto.commit=false. The code processes messages but never calls commitSync() or commitAsync(). What is the effect on message processing?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "false");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));

while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
        // Process message
    }
    // No commit called here
}
AMessages will be reprocessed after restart because offsets are never committed.
BOffsets are committed asynchronously by default even if auto-commit is false.
CThe consumer will throw a runtime exception due to missing commit calls.
DMessages will be processed exactly once because offsets are committed automatically.
Attempts:
2 left
💡 Hint

Consider what happens if offsets are never saved.

🧠 Conceptual
advanced
2:00remaining
Why choose manual commit over auto-commit in Kafka consumers?

Which of the following is the best reason to use manual commit instead of auto-commit in Kafka consumers?

ATo ensure offsets are committed only after successful processing of messages, avoiding data loss or duplication.
BTo reduce network traffic by committing offsets less frequently.
CBecause auto-commit is deprecated and no longer supported in Kafka.
DTo automatically retry failed message processing without manual intervention.
Attempts:
2 left
💡 Hint

Think about reliability and message processing guarantees.

Predict Output
advanced
2:00remaining
What error occurs if commitSync() is called outside poll loop?

Given a Kafka consumer with enable.auto.commit=false, what happens if commitSync() is called before any poll() call?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "false");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

// Calling commitSync before poll
consumer.commitSync();
AThrows TimeoutException because commitSync waits for poll to assign partitions.
BNo error; commitSync commits the last committed offsets successfully.
CThrows IllegalStateException because no offsets have been fetched yet.
DThrows NullPointerException due to missing consumer records.
Attempts:
2 left
💡 Hint

Consider what commitSync needs before it can commit offsets.

🚀 Application
expert
3:00remaining
How to implement exactly-once processing with manual commit in Kafka consumer?

You want to process Kafka messages exactly once with manual commit. Which approach below correctly ensures that offsets are committed only after successful processing?

AProcess messages, then call <code>commitSync()</code> immediately after processing each message inside the for-loop.
BProcess all messages in the batch, then call <code>commitSync()</code> once after the for-loop completes successfully.
CCall <code>commitSync()</code> before processing messages to reserve offsets.
DEnable auto-commit and rely on Kafka to commit offsets after processing.
Attempts:
2 left
💡 Hint

Think about committing offsets only after all messages are processed successfully.