Video in progress

How do the AckMode types in Spring Kafka differ, and when do you need MANUAL?

When it commits — BATCH: after the whole poll result is processed (default); RECORD: after each record: many commits, slower; TIME / COUNT: by time or by count; MANUAL: after acknowledge(), batched, before the next poll; MANUAL_IMMEDIATE: as soon as acknowledge() is called

📖 Answer

AckMode When it commits
BATCH after the whole poll result is processed (default)
RECORD after each record: many commits, slower
TIME / COUNT by time or by count
MANUAL after acknowledge(), batched, before the next poll
MANUAL_IMMEDIATE as soon as acknowledge() is called
@KafkaListener(topics = "shipments", groupId = "tracker")
public void on(ShipmentEvent e, Acknowledgment ack) {
    if (tracker.handle(e)) {
        ack.acknowledge();
    } else {
        ack.nack(Duration.ofSeconds(2)); // it can fetch again starting from this record
    }
}

For MANUAL, set factory.getContainerProperties().setAckMode(AckMode.MANUAL) on the factory, or spring.kafka.listener.ack-mode: manual.

In practice I start with BATCH and rely on the error handler. The cases where you need MANUAL are rare: when you deliberately have to skip a message without acking it, or when the signal that the work is finished comes from somewhere else. With MANUAL, forgetting acknowledge() in a single branch is a source of messages that come back on the next restart.