Video in progress

Strong Junior 📨 Kafka and messaging

What problems can you run into with enable.auto.commit=true?

Auto-commit commits offsets on a timer (every 5 seconds by default) and doesn't care whether the message has actually been processed. As a result, after a crash messages can be lost or delivered again.

📖 Answer

With a plain KafkaConsumer, auto-commit happens inside poll() and commits the offsets received in the previous poll. There are two bad scenarios:

  1. Duplicates. 100 messages arrived, 70 were processed, and the pod died 2 seconds before the commit. After the restart the same 100 come again.
  2. Loss. If you hand the messages off to another thread yourself, poll() keeps going and the offset gets committed. If the application goes down before the background thread has done its work, the message is lost.

In Spring Kafka the situation is different: by default Spring Boot sets enable.auto.commit=false and the container commits the offset itself (AckMode.BATCH, after all records from the poll have gone through the listener). So in a Spring application don't turn enable-auto-commit: true on by hand.

Either way, at-least-once is the realistic guarantee in Kafka. Write your consumer so that it tolerates duplicates.