Exactly-Once Semantics: Scope and Limits
The problem in plain words
Networks drop replies. A producer sends a message, the broker saves it, and the "got it" reply is lost. The producer can't tell whether the message was saved, so it sends it again. A consumer has the same problem: it processes a message, crashes before saving its bookmark (the offset), restarts, and processes the same message again.
That gives three possible guarantees:
| Guarantee | In plain words | What goes wrong |
|---|---|---|
| At-most-once | Send once and never retry | Messages can be lost |
| At-least-once | Retry until you get a reply | Messages can be duplicated |
| Exactly-once | Retry until you get a reply, and the system spots and drops the repeats | Holds only where the system can spot the repeats |
Exactly-once is not magic delivery. It's at-least-once plus deduplication. That only works where something can recognise a repeat, and Kafka can only recognise repeats inside its own logs.
What EOS covers: two pieces
| Piece | Config | In plain words | What it actually does |
|---|---|---|---|
| Idempotent producer | enable.idempotence=true(default since Kafka 3.0) | "If I send the same thing twice by accident, store it once." | Every producer gets an ID (PID), and every batch gets a sequence number for each partition. The broker drops any batch whose (PID, sequence) it has already written. This covers retries for one partition within one producer session. |
| Transactions | transactional.id on the producerisolation.level=read_committed on consumers | "These writes all happen together, or none of them do." | Writes to several partitions or topics, plus the consumer's offset commit, become one atomic unit. Consumers set to read_committed never see writes from a transaction that was aborted or is still open. |
Together they make the read → process → write-back-to-Kafka loop exactly-once. Kafka Streams turns all of this on with one setting: processing.guarantee=exactly_once_v2. The mechanics are in the Exactly-once mechanism part.
Transactions, unpacked: who does what
Why this is not a distributed transaction across services. Every write in the transaction goes to Kafka, from one producer in one process. That includes the offset commit, which is just a write to the internal __consumer_offsets topic. "Write to 2 topics + commit the offset" really means "write to 3 Kafka topics", and Kafka can coordinate that because it owns all three. Your code only calls beginTransaction, send, sendOffsetsToTransaction and commitTransaction. Kafka Streams does even that for you.
| Side | Config / API | Its job |
|---|---|---|
| Producer (most of the work) | transactional.id, begin/commit, sendOffsetsToTransactioninput consumer has enable.auto.commit=false | Writes the transaction. It commits the input offset as part of the transaction, so the consumer never commits on its own. |
| Broker (transaction coordinator) | none (internal) | Enforces the transaction. It tracks which partitions were touched, writes the COMMIT/ABORT markers, and fences zombies by epoch. |
| Downstream consumer | isolation.level=read_committed | Respects the transaction. It skips aborted records and waits at open ones. Aborted records are still physically in the log, so a default read_uncommitted consumer reads them as if they were real. |
One line for interviews: the producer writes atomically, and the consumer has to opt in to reading atomically.
Why transactional.id must be stable: a zombie timeline. The service runs as transactional.id = "payment-1".
- Instance A registers as
payment-1, and the coordinator records it as the owner at epoch 1. A begins a transaction and writes "charge ₹500". It hasn't committed yet. - A freezes in a long GC pause. The orchestrator assumes it's dead and starts instance B with the same
payment-1. - The coordinator finds an open transaction from epoch 1 and aborts it, so A's half-written charge gets an ABORT marker. It then makes B the owner at epoch 2.
- B reprocesses the order from the last committed offset, writes the charge and commits. ✅
- A wakes up and tries to commit as epoch 1. The broker rejects it with
ProducerFencedException, because epoch 1 is older than epoch 2. ❌ The customer is charged exactly once.
If B had registered under a random ID, Kafka couldn't tell that B replaces A. A's transaction would stay open until it timed out, A's late commit would succeed, and the customer would be charged twice. Think of a hotel key card: when reception issues a new card for the same room, the old card stops working. The full protocol is in the Exactly-once mechanism part.
What EOS does NOT cover
The rule: if the effect of processing lands outside Kafka, Kafka can't undo or deduplicate it.
| Scenario | What happens, in plain words | Why EOS can't help | What to do |
|---|---|---|---|
| Consumer writes to a DB, calls an API or sends an email | The consumer charges the card, then crashes before committing its offset. On restart it reads the same message and charges the card again. | Kafka's transaction can roll back Kafka writes. It can't roll back a Postgres row or a Stripe charge. | Make the sink idempotent: use an upsert keyed by event ID, a dedup table, an idempotency key on the API call, or store the offset in the same DB transaction as the result. |
| Data copied to a second cluster (MirrorMaker, DR) | Cluster A and cluster B each have their own receipt book. Neither knows what the other has seen. | Transactions only cover one cluster. | Expect duplicates on the copy and deduplicate downstream by event ID. |
| Producer crashes mid-transaction | Half a delivery sits in the back room. Kafka aborts it, so nothing partial is visible, but the work isn't done either. | Abort keeps the data clean, but it doesn't redo the work. | Restart with the same transactional.id. The new instance blocks the old "zombie" one and aborts its open transaction, and the app reprocesses from the last committed offset. |
| Consumer rebalance before the offset commit | A partition moves to another consumer before the first one saved its bookmark. The new owner starts from the old bookmark and repeats some messages. | This is only safe if the offsets were committed inside the transaction (sendOffsetsToTransaction). A plain consumer that commits on its own will reprocess. | Use the transactional consume-transform-produce loop (or Kafka Streams EOS). Otherwise make processing idempotent. |
| Non-Kafka producer, or idempotence switched off | Retries write real duplicates into the log. | The receipt book only exists if the producer writes the numbers on the parcels. | Keep enable.idempotence=true, and put a business event ID in every message. |
Worked example: a payment event
- Producer → Kafka. The payment service publishes
PaymentCreated{id: p-123}. The ack is lost and it retries. Idempotence makes sure the log holds p-123 only once. ✅ Kafka handles this. - Kafka → Kafka. A fraud scorer reads p-123 and writes
FraudScore{p-123, low}to another topic, with its offset commit in the same transaction. If it crashes halfway, the whole thing is aborted and redone, and downstream sees exactly one score. ✅ Kafka handles this. - Kafka → outside world. A ledger consumer reads p-123 and inserts a row in Postgres. If it crashes after the insert but before the offset commit, it will see p-123 again. ❌ Kafka can't help here. The fix is
INSERT … ON CONFLICT (payment_id) DO NOTHING, so the second insert does nothing.
The honest answer: Kafka EOS means exactly-once within Kafka: no duplicate writes into the log, and atomic read-process-write between Kafka topics. End-to-end exactly-once needs one more thing: every external side effect has to be idempotent (upserts, dedup keys or idempotency tokens at the sink). Interview one-liner: "Exactly-once is at-least-once delivery plus deduplication. Kafka deduplicates its own log. Anything that leaves Kafka has to deduplicate itself."