Quick revision
The short answer
Spring Kafka's listener container sets enable.auto.commit=false unless we set it explicitly, so that offsets are committed by the container after processing rather than by a timer in the client. The default AckMode is BATCH, which commits the offsets of a poll after all its records have been processed; RECORD commits after each record, and MANUAL and MANUAL_IMMEDIATE leave it to the listener to call Acknowledgment.acknowledge(), with MANUAL queuing the commit until the batch is done and MANUAL_IMMEDIATE committing at once. Committing after processing gives at-least-once delivery, whereas Kafka's own auto-commit can commit offsets before processing has finished, which can lose records on a crash.
What to remember
In this answer, we saw that Spring Kafka's container sets enable.auto.commit=false unless configured otherwise, so commits follow processing instead of a timer that can commit unprocessed records. The default AckMode BATCH commits a poll's offsets after all its records are processed, RECORD commits per record, and MANUAL and MANUAL_IMMEDIATE commit only acknowledged records, queued until the batch ends or immediately. All of them give at-least-once delivery, so listeners must be idempotent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Kafka's listener container sets enable.auto.commit=false unless we set it explicitly, so that offsets are committed by the container after processing rather than by a timer in the client. The default AckMode is BATCH, which commits the offsets of a poll after all its records have been processed; RECORD commits after each record, and MANUAL and MANUAL_IMMEDIATE leave it to the listener to call Acknowledgment.acknowledge(), with MANUAL queuing the commit until the batch is done and MANUAL_IMMEDIATE committing at once. Committing after processing gives at-least-once delivery, whereas Kafka's own auto-commit can commit offsets before processing has finished, which can lose records on a crash.
Why this question is asked
Offset commits decide what happens to messages when a consumer crashes: whether they are processed again, or silently skipped. Spring Kafka hides commits behind the listener container, which is convenient, but we need to know what it does to reason about duplicates and data loss. Interviewers ask this to check that we understand the delivery guarantee our listeners actually have. In this answer, we will recap offsets, see why Spring turns off Kafka's auto-commit, go through the AckModes from BATCH to MANUAL_IMMEDIATE, and see how each gives at-least-once delivery.
1A Quick Recap of Offsets and Commits
Each record in a partition has an offset, its position in the log. A consumer group stores, per partition, a committed offset: the position of the next record to read. When a consumer starts, or takes over a partition after a rebalance, it resumes from the committed offset. So the committed offset is the group's bookmark.
When the bookmark moves relative to processing decides the guarantee. If we commit after processing, a crash between processing and commit makes the next owner process those records again: at-least-once delivery. If we commit before processing, a crash in between makes the next owner skip them: at-most-once delivery, which loses records.
2Why Spring Disables enable.auto.commit
With enable.auto.commit=true, the Kafka client commits offsets itself, every auto.commit.interval.ms (5 seconds by default), during calls to poll(). It commits the offsets of records that poll() has already returned to the application, whether or not the application has finished with them. In a simple loop that processes everything before the next poll this mostly works, but as soon as records are handed to another thread, or processing is still running when the timer fires, the client commits offsets for records that are not processed yet. If the application crashes then, those records are never processed: they are lost.
Spring Kafka's container therefore sets enable.auto.commit=false unless we configure it explicitly, and commits offsets itself at points tied to our listener's progress, chosen by the AckMode. That way the commit always means 'processed', not 'handed over'.
5 messages in order. Play them, or step through them one at a time.
Records 150 to 199 were committed but never processed.
3The Default: AckMode BATCH
With AckMode.BATCH, the default, the container polls a batch of records, calls our listener for each of them in turn, and when the whole batch has been processed, commits the offsets of that batch before polling again. If the listener throws for a record, the error handler takes over, by default retrying that record a few times and then logging it, and the committed offsets reflect where processing safely got to.
BATCH is a good default because it commits once per poll, not once per record, which keeps the cost of commits low while still committing only processed records. The window for duplicates is one batch: a crash halfway through a batch of 500 replays up to 500 records.
| AckMode | When the container commits | Typical use |
|---|---|---|
| BATCH (default) | after all records of a poll are processed | most listeners |
| RECORD | after each record is processed | slow records, small replay window |
| TIME / COUNT | after a time or a number of records | tuning commit frequency |
| MANUAL | acknowledged offsets, committed with the batch | listener decides what is done |
MANUAL_IMMEDIATE | as soon as acknowledge() is called | commit right away, at a cost |
4RECORD, MANUAL and MANUAL_IMMEDIATE
AckMode.RECORD commits after each record is processed. The replay window after a crash shrinks to one record, but there is a commit request per record, which costs throughput. It suits listeners whose records take long to process, where replaying a whole batch would be expensive.
The two manual modes hand the decision to our code. The listener declares an Acknowledgment parameter and calls acknowledge() when it considers the record done. With MANUAL, the acknowledgement is queued, and the container commits the acknowledged offsets after the current batch has been processed, like BATCH but only for acknowledged records. With MANUAL_IMMEDIATE, the container commits as soon as acknowledge() is called, if it is called on the consumer thread. Both mean that a record we never acknowledge is not committed, so it will be read again after a restart or rebalance.
Manual modes are useful when 'done' is something other than 'the method returned', for example when we want to acknowledge only after an external system confirms. The trap is forgetting to acknowledge on some path, which makes the consumer replay those records on every restart. Calling acknowledge() from another thread needs care; Spring Kafka has options for this and for out-of-order acknowledgements, so check your version's documentation.
5At-Least-Once and What It Asks of Us
Every container AckMode commits after processing, so they all give at-least-once delivery: no record is skipped, but a record may be processed more than once after a crash or rebalance. The difference between them is only the size of the replay window and the commit cost. That means our listeners must be idempotent. A payment listener records the event id in the same database transaction as the ledger entry and skips ids it has already seen; a status listener sets the status rather than counting transitions.
If we need exactly-once results within Kafka, for example consuming and producing to other topics, the container can run Kafka transactions and commit offsets as part of the transaction. For side effects outside Kafka, idempotence remains the answer.
6Follow-up questions
Can we turn on enable.auto.commit in Spring?
Yes, by setting it explicitly; the container then lets the client commit on its timer and ignores the AckMode for commits. It removes the guarantee that committed means processed, so it is only reasonable when losing a few records after a crash is acceptable, such as for some metrics.
Are commits synchronous?
By default the container commits synchronously, waiting for the broker to confirm, which makes commit failures visible. There is an option to commit asynchronously for throughput; check your version's container properties.
7Conclusion
In this answer, we saw that Spring Kafka's container sets enable.auto.commit=false unless configured otherwise, so commits follow processing instead of a timer that can commit unprocessed records. The default AckMode BATCH commits a poll's offsets after all its records are processed, RECORD commits per record, and MANUAL and MANUAL_IMMEDIATE commit only acknowledged records, queued until the batch ends or immediately. All of them give at-least-once delivery, so listeners must be idempotent.
Open with
The container takes commits away from the Kafka client. Unless I set enable.auto.commit explicitly, Spring sets it to false, because the client's auto-commit runs on a timer, every five seconds by default, and commits the offsets of whatever poll returned, whether or not processing has finished.
Then, if they want more
If work is still running or was handed to another thread and the app crashes, those records were committed but never processed, so they are lost. Instead the container commits based on the AckMode.
The default is BATCH: it polls, calls my listener for each record, and once the whole batch is processed it commits the offsets before the next poll. RECORD commits after every record, which shrinks the replay window to one record but costs a commit per record.
With MANUAL and MANUAL_IMMEDIATE, my listener takes an Acknowledgment parameter and calls acknowledge when the record is really done; with MANUAL the acknowledged offsets are committed after the batch, and with MANUAL_IMMEDIATE the commit happens right away on the consumer thread.
In all these modes the commit comes after processing, so the guarantee is at-least-once: a crash between processing and commit means those records are processed again by the next owner. So I make listeners idempotent, for example by storing processed event ids in the same database transaction, and if I need exactly-once within Kafka I use container-managed Kafka transactions.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Spring Kafka's listener container commits offsets for you. Explain the default AckMode...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/2829c49b38/