What are the dangers of enable.auto.commit in Kafka?
`enable.auto.commit=true` (the default) instructs the Kafka consumer to automatically commit the current offset to the broker on a fixed interval controlled by `auto.commit.interval.ms` (default 5 seconds). While convenient, this creates two distinct failure modes that are both correctness problems in most production systems.
The first danger is message loss. Auto-commit advances the offset based on a timer, not on whether processing succeeded. If the consumer fetches a batch, the auto-commit fires, and then the consumer crashes before finishing processing, the committed offset has moved forward. On restart, those messages are skipped — silently lost from the consumer's perspective. For any system where missing a message is a bug (financial events, audit logs, state transitions), this is unacceptable.
The second danger is unintended duplicate processing combined with offset amnesia. Suppose processing takes longer than `auto.commit.interval.ms`. The consumer is mid-batch when the commit fires. If the consumer then fails after the commit but before completing downstream writes, it restarts at the committed point — which is past the beginning of the batch but not necessarily past the failed message. Depending on batch boundaries, this can cause inconsistent partial processing.
The safe alternative is manual offset commit: call `commitSync()` or `commitAsync()` only after processing and any downstream writes are complete. `commitSync()` blocks and retries on failure, providing at-least-once semantics with the retry window fully under application control. `commitAsync()` is non-blocking but requires a callback to handle failures — typically used for throughput-sensitive paths where the application can tolerate the last offset being recommitted.
For exactly-once semantics, neither auto nor manual commit alone is sufficient. The pattern is to write both the processed result and the new offset atomically to a transactional datastore — offset commit and business write happen in the same transaction, making them inseparable.
Identifies both failure modes (loss and duplicates), explains that auto-commit is timer-based not processing-based, and recommends manual commit as the fix.
Distinguishes commitSync vs commitAsync trade-offs, articulates the exactly-once pattern (transactional offset + result write), and gives a concrete failure scenario demonstrating the timing gap between commit and processing completion.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment