# Exactly-Once Delivery: Myth, Reality, and What to Do Instead

URL: https://www.devopsness.com/blog/exactly-once-delivery
Author: Kiril Urbonas
Published: 2026-08-16
Updated: 2026-10-31
Categories: DevOps
Tags: Delivery Semantics, Idempotency, Event-Driven, Backend, Message Queues

Summary: Exactly-once network delivery is impossible, but exactly-once processing is achievable through at-least-once delivery plus idempotent consumers.

---
# Exactly-Once Delivery: Myth, Reality, and What to Do Instead

Exactly-once delivery over a network is impossible: a sender can't distinguish a lost message from a lost acknowledgment, so it must retry (risking duplicates) or give up (risking loss). What you can build is exactly-once processing: at-least-once delivery plus idempotent consumers that commit a dedup key in the same transaction as the side effect.

> [!VERIFIED]
> Apache Kafka 4.4 (4.4.0, released 2026-10-22), PostgreSQL 18, against the Kafka 4.4 design and producer configuration docs, KIP-98, and the PostgreSQL 18 `INSERT ... ON CONFLICT` reference. Checked October 2026.

Chasing an exactly-once delivery guarantee usually produces a system that is slower, more fragile, and still occasionally double-processes. The useful move is to understand why the guarantee cannot exist, then design for what you can build.

## The three delivery semantics

Messaging systems offer one of three contracts between producer and consumer.

**At-most-once:** the message is delivered zero or one times. The producer sends and forgets, or the broker drops on failure rather than retrying. You never see duplicates, but you can silently lose messages. Fine for metrics samples or fire-and-forget telemetry, wrong for anything involving money or state changes.

**At-least-once:** the message is delivered one or more times. The system retries until it gets an acknowledgment, so nothing is lost, but a lost or delayed ack means the same message arrives again. This is the default and the workhorse of nearly every serious queue; Kafka's own design docs state it guarantees at-least-once by default.

**Exactly-once:** the message is processed once and only once, no loss and no duplicates. This is what everyone wants and what marketing pages love to promise. Kafka's documentation itself warns that many "exactly-once" claims don't survive the fine print once producers or consumers can fail.

## Why true exactly-once delivery is impossible

The blocker is the two generals problem. Imagine a producer sending a message and waiting for an acknowledgment. The message arrives, the consumer processes it, and the ack travels back. If that ack is lost in the network, the producer has no way to distinguish "consumer never got it" from "consumer got it but the ack vanished." Its only safe options are to retry, which risks a duplicate, or to give up, which risks a loss.

No amount of extra handshaking fixes this. Every acknowledgment needs its own acknowledgment, an infinite regress. This is not an engineering gap a better broker will close; it is a proven property of communication over an unreliable channel.

Since silent loss is usually worse than a duplicate, mature systems pick at-least-once and deal with the duplicates.

## What exactly-once really means in practice

When a vendor says exactly-once, they mean exactly-once *processing*, not delivery. The message may physically arrive several times, but the observable effect on your system happens once. Duplicates still hit the wire; the consumer recognizes and discards them. The guarantee lives at the processing boundary, not the transport.

You cannot stop duplicates from arriving, but you can make a second arrival a no-op. That is idempotency, and it is the real answer.

## Idempotent consumers

An operation is idempotent when applying it twice has the same effect as applying it once. Some operations are naturally idempotent: setting a user's status to `active`, or overwriting a record with an absolute value. Others are not: incrementing a balance, appending to a ledger, sending an email.

For the non-idempotent cases, you attach an idempotency key to each message. The producer generates a stable unique ID (an order ID, a UUID chosen at creation and preserved across retries) and the consumer records which keys it has already handled in a dedup store. Before acting, it checks the store. Seen the key, skip the work. New key, do the work and record the key.

The subtlety is atomicity. Recording the key and performing the side effect must commit together, or you reopen the same gap you were closing. If you process first and record second, a crash between them leaves the key unrecorded and the next delivery reprocesses. The clean version keeps the dedup record and the business write in one transaction.

```sql
-- migrations/001_processed_messages.sql
CREATE TABLE processed_messages (
    idempotency_key TEXT PRIMARY KEY,
    processed_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);
```

```python
# consumer/handlers.py
def handle_payment(conn, msg):
    key = msg["idempotency_key"]
    with conn:  # single transaction: dedup check + side effect commit together
        cur = conn.cursor()
        cur.execute(
            "INSERT INTO processed_messages (idempotency_key) "
            "VALUES (%s) ON CONFLICT DO NOTHING",
            (key,),
        )
        if cur.rowcount == 0:
            return  # duplicate: key already present, do nothing

        cur.execute(
            "UPDATE accounts SET balance = balance - %s WHERE id = %s",
            (msg["amount"], msg["account_id"]),
        )
    ack(msg)
```

The `ON CONFLICT DO NOTHING` plus `rowcount` check makes the insert the gate. If the key is already there, `rowcount` is zero and the balance update never runs. Because both statements share one transaction, a crash rolls back the whole thing and redelivery retries cleanly. The processed-id table is the dedup store; the primary key does the deduplication for you.

## The transactional outbox pattern

Idempotency handles the consumer side. The producer side has a matching hazard: you update your database and then publish an event, and a crash between the two steps loses the event or, if reordered, publishes without persisting. The outbox pattern collapses those into one atomic write. You insert the event into an `outbox` table in the same transaction as your business update, then a separate relay process reads the outbox and publishes to the broker, marking rows as sent. The relay retries freely, which means the broker gets at-least-once, which is exactly why the consumer needs to be idempotent. Messages that fail repeatedly for non-transient reasons belong in a [dead-letter queue](/blog/dead-letter-queues), not in an infinite retry loop that blocks the partition. The two patterns are complementary halves of the same design.

## Kafka's exactly-once semantics

Kafka is the system most often cited as delivering exactly-once, and it genuinely does within its own boundary. Two mechanisms combine. The idempotent producer tags each batch with a producer ID and sequence number so the broker discards duplicate writes from retries; as of Kafka 4.4, `enable.idempotence` is on by default unless conflicting settings (such as `acks` other than `all`) turn it off. Transactions let you write to multiple partitions and commit consumer offsets atomically, so a consume-transform-produce loop either fully commits or fully aborts. Downstream consumers must read with `isolation.level=read_committed`, or they will see records from aborted transactions anyway.

The limit is the phrase "within Kafka's boundary." The guarantee holds when your inputs and outputs are both Kafka topics and your processing state lives in Kafka. The moment your consumer writes to an external database, calls a payment API, or sends an email, you have stepped outside the transactional fence, and Kafka can no longer make it atomic. For those side effects you are back to idempotency keys and a dedup store. The Kafka docs say as much: exactly-once into other systems "generally requires cooperation with such systems." Partitioning by the idempotency key helps here, since all retries for one key land on one consumer, as covered in [Kafka partition strategies for scaling consumers](/blog/kafka-partition-strategies-scaling-consumers). Kafka exactly-once is real and useful for stream processing pipelines; it is not a blanket exemption from thinking about duplicates.

## Questions people ask

### Does Kafka support exactly-once delivery?

Within Kafka, yes. The idempotent producer removes duplicates caused by retries, and transactions commit output records and consumer offsets atomically, so a read-process-write loop between Kafka topics is exactly-once when consumers use `read_committed`. Kafka Streams wraps this for you. The guarantee ends at Kafka's boundary: writes to a database, an HTTP API or an email service still need idempotency keys.

### What is the difference between exactly-once delivery and exactly-once processing?

Delivery is about how many times a message crosses the network; processing is about how many times its effect lands in your system. Delivery can't be guaranteed once and only once over an unreliable network. Processing can, by accepting duplicate deliveries and making the second one a no-op through idempotent operations or a dedup table committed with the business write.

### How long should I keep idempotency keys?

At least as long as a duplicate can plausibly arrive: the broker's maximum redelivery window plus your longest replay or backfill. For most queues that is hours to days, so a TTL of a week is a safe default. If you ever replay a topic from the beginning, either keep keys for the full retention period or make the handler naturally idempotent.

## The call we'd make

Design for at-least-once and make every consumer idempotent. It is the one combination that loses nothing and tolerates the duplicates the network will inevitably produce. Assign a stable idempotency key at the edge, keep a processed-id table, and commit the dedup record in the same transaction as the side effect. Add the outbox pattern on the producer side so publishes never diverge from your database. Reach for Kafka transactions only when the whole pipeline stays inside Kafka, and treat any external call as a place where you own the dedup. For the wider context on how these events flow between services, see our guide to [event-driven architecture](/blog/event-driven-architecture-guide). Stop hunting for a delivery guarantee that cannot exist and build the processing guarantee that can.

## Sources

- [Apache Kafka 4.4 documentation: Design, message delivery semantics](https://kafka.apache.org/44/design/design/)
- [Apache Kafka 4.4 documentation: Producer configs (enable.idempotence)](https://kafka.apache.org/44/configuration/producer-configs/)
- [KIP-98: Exactly Once Delivery and Transactional Messaging](https://cwiki.apache.org/confluence/spaces/KAFKA/pages/66854913/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging)
- [PostgreSQL 18 documentation: INSERT and ON CONFLICT](https://www.postgresql.org/docs/current/sql-insert.html)

