Video in progress

What does Kafka's "exactly-once" guarantee actually cover, and what does it not cover?

Through the idempotent producer and transactions, Kafka EOS makes the offset commit and the outgoing messages atomic in a "read from Kafka, write to Kafka" chain. External side effects like writing to a DB, an HTTP call or an email are not part of this guarantee.

📖 Answer

There are three parts:

  1. The idempotent producer (enable.idempotence=true): when the producer retries, the broker drops the duplicate by sequence number. This holds within one producer session and one partition.
  2. Transactions (transactional.id): writing to several partitions and committing the consumer offset is one atomic action. Either everything is visible or none of it.
  3. isolation.level=read_committed on the consumer side: it doesn't see messages from aborted transactions.

In Kafka Streams, processing.guarantee=exactly_once_v2 wires all of this up automatically. The result: "read from topic A, compute, write to topic B" really does take effect once per message.

What stays outside the guarantee:

@KafkaListener(topics = "payments")
@Transactional("kafkaTransactionManager")
public void on(PaymentEvent e) {
    jdbc.update("UPDATE account SET balance = balance - ? ...", e.amount()); // not covered by EOS
    kafka.send("ledger", e.id(), toLedger(e));                                // covered
}

If the Kafka transaction aborts, the ledger message isn't visible, the offset goes back and the message comes again. But the UPDATE in the DB has already committed, so the balance gets decreased twice.

That is why, when working with an external system, I rely not on "exactly-once" but on "effectively once": at-least-once delivery + idempotent processing (a dedup table, a business key, a conditional update). Storing the offset in the DB itself, in the same transaction as the business data, is also an option: then the DB is the source of truth, not the Kafka offset.

In an interview, "Kafka gives exactly-once, so there will be no duplicates" is a red flag. The right answer is: within which boundary and under which conditions.