Skip to content
Kafka Is Fun

SpringSpring Kafka Configuration

How do you set up manual offset commits in Spring Kafka?

Reference answer

Set spring.kafka.listener.ack-mode to manual or manual_immediate (the container AckMode MANUAL or MANUAL_IMMEDIATE) and take an Acknowledgment parameter in the @KafkaListener method, for example listen(String data, Acknowledgment ack), then call ack.acknowledge() when processing is done. Without a manual AckMode the Acknowledgment object is null. With MANUAL, the listener is responsible for calling acknowledge() and after that BATCH semantics apply, meaning the offset is committed once all records returned by poll() have been processed. With MANUAL_IMMEDIATE the offset is committed immediately when acknowledge() is called. The default AckMode is BATCH; the other modes are RECORD, TIME, COUNT and COUNT_TIME. A batch listener can take the Acknowledgment too (BatchAcknowledgingMessageListener). Since Spring Kafka 4.1 the ackMode attribute on @KafkaListener can override the factory's default.

Expected key concepts: spring.kafka.listener.ack-mode, MANUAL, MANUAL_IMMEDIATE, Acknowledgment, acknowledge, BATCH, poll, AckMode, @KafkaListener

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.