Video in progress

Strong Junior 📨 Kafka and messaging

Explain the difference between at-most-once, at-least-once and exactly-once delivery.

At-most-once means a message can be lost but is never repeated. At-least-once means it isn't lost but can be repeated. Exactly-once means the effect of each message happens once; Kafka gives you this only under certain conditions.

📖 Answer

On the consumer side the difference is the order of commit and processing:

// at-most-once: commit first, then process
consumer.commitSync();
process(records);      // crash here: the message is lost

// at-least-once: process first, then commit
process(records);
consumer.commitSync(); // crash before the commit: the message comes again

In practice almost everyone picks at-least-once and makes the consumer idempotent: for example, by storing the event ID with a unique constraint in a processed_events table.

In Kafka, exactly-once works for the "read from Kafka, write to Kafka" chain: idempotent producer + transaction + isolation.level=read_committed. But if inside the listener you send an HTTP request to an external API or send an email, a Kafka transaction can't roll that back. You still need idempotency there.

If you answer "Kafka gives you exactly-once" in an interview and stop there, the next question will be about exactly these limits.