What if your program lost track of what it processed and repeated or missed important data?
Auto-commit vs manual commit in Kafka - When to Use Which
Imagine you are reading messages from a Kafka topic and you have to keep track of which messages you have processed by yourself. You write down the last message number manually every time you finish processing a batch.
This manual tracking is slow and risky. If your program crashes or you forget to update the message number, you might process the same messages twice or miss some messages entirely. It's hard to keep everything perfectly in sync.
Auto-commit and manual commit in Kafka solve this problem by managing the message tracking for you. Auto-commit automatically saves your progress at intervals, while manual commit lets you decide exactly when to save, giving you more control and safety.
processMessages(); saveOffsetManually();
consumer.commitSync(); // manual commit
// or
consumer.enableAutoCommit = true; // auto-commit enabledThis concept lets your program reliably know which messages are processed, preventing duplicates or losses, and making your data processing smooth and safe.
Think of a bank processing transactions from a Kafka stream. Using manual commit, the bank ensures each transaction is fully processed before marking it done, avoiding errors or double charges.
Manual commit requires you to track progress yourself, which can be error-prone.
Auto-commit automatically saves progress but may commit too early.
Manual commit gives you control to commit only after successful processing.
Practice
Solution
Step 1: Understand auto-commit behavior
Auto-commit commits offsets automatically at regular intervals without user intervention.Step 2: Understand manual commit behavior
Manual commit requires the consumer to explicitly call commit() after processing messages.Final Answer:
Auto-commit commits offsets automatically at intervals, manual commit requires explicit commit calls. -> Option CQuick Check:
Auto-commit = automatic, Manual commit = explicit call [OK]
- Confusing which method commits automatically
- Thinking manual commit disables offset tracking
- Believing auto-commit commits only on shutdown
Solution
Step 1: Identify the correct config key
The correct Kafka consumer config key to control auto-commit is "enable.auto.commit".Step 2: Identify the correct value to disable auto-commit
Setting "enable.auto.commit" to false disables auto-commit.Final Answer:
"enable.auto.commit" : false -> Option AQuick Check:
Disable auto-commit = enable.auto.commit = false [OK]
- Using wrong config key names
- Setting true instead of false to disable
- Using string values instead of boolean
consumer.poll(Duration.ofMillis(100)).forEach(record -> {
process(record);
});
consumer.commitSync();
What happens if commitSync() is called before processing the records?Solution
Step 1: Understand commitSync timing
commitSync() commits the current offsets to Kafka immediately when called.Step 2: Analyze calling commitSync before processing
If commitSync() is called before processing, offsets are saved before messages are handled, risking data loss if processing fails.Final Answer:
Offsets are committed before processing, risking message loss on failure. -> Option AQuick Check:
Commit before processing = risk of message loss [OK]
- Assuming commitSync waits for processing
- Thinking commitSync throws error if called early
- Believing offsets commit automatically without call
Solution
Step 1: Check manual commit requirements
When auto-commit is disabled, the consumer must call commit() explicitly after processing.Step 2: Identify cause of missing commits
If offsets are not committed, likely commit() was not called after processing, causing reprocessing.Final Answer:
You forgot to call commit() after processing messages. -> Option DQuick Check:
Manual commit needs explicit commit() call [OK]
- Assuming auto-commit still works when disabled
- Blaming Kafka broker without checking code
- Calling commit() before processing messages
Solution
Step 1: Understand exactly-once processing needs
Exactly-once means processing each message once and committing offset only after success.Step 2: Compare commit strategies
Auto-commit commits offsets automatically at intervals, risking committing before processing completes. Manual commit lets you commit only after successful processing.Final Answer:
Manual commit, because you commit offsets only after successful processing. -> Option BQuick Check:
Exactly-once needs manual commit after success [OK]
- Assuming auto-commit guarantees exactly-once
- Committing offsets before processing
- Ignoring commit timing importance
Ready to prove it?
Pass the test and this pattern moves unknown
