Sub topic 9
Offset commits and acknowledgment modes
This chapter assumes you can write a basic @KafkaListener and know that a consumer group tracks its position in each partition. It covers how that position is saved, when Spring saves it, and how to take control of the timing.
What a committed offset is
A committed offset is the position a consumer group has saved for one partition. Kafka stores it in an internal topic called __consumer_offsets. The saved value is the offset of the next record to read, not the last one processed.
After a restart or a rebalance, the group resumes from the committed offset. The commit therefore decides what gets read again and what gets skipped:
- If you commit before processing and then crash, the record is lost to your application.
- If you process before committing and then crash, the record is delivered again.
The second behavior is called at-least-once delivery. It is the usual choice, and it means your listener should tolerate duplicates.
Who commits: Kafka or Spring
The Kafka client can commit on a timer when enable.auto.commit is true. That timer knows nothing about whether your listener finished. Spring Kafka avoids this problem by turning client auto-commit off and committing from its listener container after your method returns. The container's acknowledgment mode controls when that happens.
Acknowledgment modes
You set the mode with spring.kafka.listener.ack-mode.
| Mode | When offsets are committed |
|---|---|
| `BATCH` (default) | After all records from one `poll()` have been processed |
| `RECORD` | After each record's listener returns |
| `TIME` | After a batch, once `ack-time` has elapsed |
| `COUNT` | After a batch, once `ack-count` records have been processed |
| `COUNT_TIME` | When either the time or the count condition is met |
| `MANUAL` | When your code calls `acknowledge()`. The commit is queued and sent with the next batch commit |
| `MANUAL_IMMEDIATE` | Immediately when your code calls `acknowledge()` |
RECORD commits most often, which narrows the window of repeated records but adds commit traffic. BATCH commits least often and is a good default. In every mode, a listener that throws an exception does not have its record committed. The error handler decides what happens next (see the error handling chapter).
Manual acknowledgment
Use a manual mode when "the method returned" is not the same as "the work is done". An example is a listener that hands the record to another thread or waits for a database write to be confirmed.
Configure the mode and make sure client auto-commit is off:
spring:
kafka:
consumer:
enable-auto-commit: false
listener:
ack-mode: manual_immediateThen add an Acknowledgment parameter to the listener and call it when the work is complete:
package com.example.orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
@Component
public class ManualAckOrderListener {
private static final Logger log = LoggerFactory.getLogger(ManualAckOrderListener.class);
@KafkaListener(topics = "orders", groupId = "order-service")
public void onOrder(OrderEvent event, Acknowledgment ack) {
log.info("Saving order {}", event.orderId());
// ... write to the database here ...
ack.acknowledge();
}
}Calling acknowledge() is the last step. If an exception is thrown first, the offset is not committed.
The commit sequence
Common errors
- Forgetting to acknowledge. In a manual mode, no
acknowledge()call means no commit. The group appears to work, but after a restart it re-reads everything since the last commit. - Acknowledging too early. Calling
acknowledge()before the work finishes turns at-least-once into at-most-once. - Acknowledging out of order. An offset commit covers everything before it in that partition. Acknowledging record 12 while record 10 is still running means a crash skips record 10.
- Setting
ack-mode: manualbut not injectingAcknowledgment. The listener runs, but nothing is ever committed. - Assuming exactly-once. Commit modes give at-least-once at best. Exactly-once needs idempotent handling or transactions, which a later chapter covers.
Exercise
A listener uses ack-mode manual. It runs a database insert, then calls ack.acknowledge(). The application crashes after the insert succeeds but before acknowledge() runs. What happens to that record after restart, and what does your listener need to cope with it?
Show answer
The offset was never committed, so the group resumes from the previous committed offset and the record is delivered again. The listener must be idempotent, for example by using the order ID as a unique key so a repeated insert is ignored or treated as an update.