“Exactly-once” is the single most asked-for, most-misunderstood feature in message-broker discussions. This article is the framework we use with clients to make the actual decision: what each delivery guarantee really means at the wire and at the application, where exactly-once is genuinely available (Kafka transactions, Pulsar transactions, JetStream with the right config), what it costs in throughput and complexity, and why the right answer for most workloads is “at-least-once with idempotent consumers.”
How to verify
There is nothing to install — this article is a decision framework. But before reaching for exactly-once on any specific broker, confirm the version and config:
# Kafka — exactly-once needs transactions (3.x default) and idempotent producer
/opt/kafka/bin/kafka-configs.sh --bootstrap-server kafka:9092 --entity-type brokers --all \
--describe | grep -E 'transaction|idempotence'
# Pulsar — transactions are opt-in
bin/pulsar-admin brokers get-runtime-config | grep -E 'transaction'
# JetStream — explicit dedup window per stream
nats stream info ORDERS | grep -E 'Duplicate'
If the broker is older than the version that introduced its exactly-once support, you have at-least-once whether you wanted it or not.
What the guarantees actually mean
At-most-once. The producer fires the message; if anything fails, the message is gone. Lowest latency, no retry overhead, but message loss is possible. The right choice for telemetry where missing a few samples does not matter — log heartbeats, dropped-frame counters, traces with sampling. Wrong choice for orders or payments.
At-least-once. The producer retries until the broker acks; the broker retries delivery to the consumer until the consumer acks. Duplicates are possible — a network blip between consumer-processed-the-message and consumer-acked-the-broker results in the broker re-delivering. The application must be ready for this. This is the default for nearly every broker — RabbitMQ confirms, Kafka producer with acks=all and consumer with manual commit, JetStream pull consumers with explicit ack, MQTT QoS 1.
Exactly-once. The combination of:
- Idempotent producer — duplicate publishes of the same message ID are deduplicated by the broker.
- Transactional commit — the produce-and-consumer-offset-update is atomic; either both happen or neither does.
- Read-committed isolation on the consumer — the consumer only sees messages that are part of a committed transaction.
When all three are in place, the broker guarantees exactly-once for messages that stay inside the broker. The moment a side effect leaves the broker — write to a database, call to an external API, send an email — the guarantee is broken unless the external system is also idempotent or transactional.
This is the part most discussions skip. “Exactly-once between Kafka and Kafka” via Kafka Streams transactions is real. “Exactly-once email send” is not, because the SMTP server has no concept of a transaction with the broker.
The decision
Use at-least-once with idempotent consumers for almost everything. The consumer takes the message, performs its side effect, and acks. If a duplicate arrives, the consumer detects it (via a deduplication store: a database row keyed on message ID, a Redis SET with TTL, a Kafka compacted topic) and acks without re-processing. This is reliable, performant, and bounded — you set the dedup window to your maximum reasonable redelivery window plus a margin.
Use exactly-once with broker transactions when the consumer’s side effect is another publish to the same broker. Kafka Streams aggregating events into output topics is the canonical case — processing.guarantee=exactly_once_v2 makes the input-aggregate-output cycle atomic.
Use at-most-once for telemetry where loss is acceptable. Set acks=0 on the producer and skip the retry path entirely. Throughput goes up, complexity goes down.
The wrong answer is “exactly-once everywhere because losing data is bad.” Exactly-once with external side effects is a lie — what you actually have is at-least-once with broker-side dedup plus whatever idempotency you built in the consumer. Be honest about that and design accordingly.
How to make at-least-once safe
The pattern that works:
-
Producer sets a deterministic message ID. For an order, this is the order ID. For an event, this is a hash of the immutable fields. Never use a UUIDv4 generated at send time — a retry generates a new UUID and breaks dedup.
-
Broker does what it does. If the broker supports producer-side dedup (Kafka idempotent producer, JetStream
Nats-Msg-Id, RabbitMQ with a custom header + plugin), enable it as a first line of defense. -
Consumer processes the message inside a database transaction that also writes the message ID into a
processed_messagestable:BEGIN; INSERT INTO processed_messages (msg_id, processed_at) VALUES ($1, NOW()) ON CONFLICT (msg_id) DO NOTHING RETURNING msg_id; -- if the RETURNING is empty, the message was already processed — skip -- otherwise, perform the side effect (insert order, update balance, etc.) -- in the SAME transaction COMMIT; -
Consumer acks the broker only after the database transaction commits successfully. A crash between the commit and the ack causes the broker to redeliver, but the next attempt’s INSERT hits the unique constraint and the consumer short-circuits.
This is “effectively-once” — duplicates are possible at the wire, idempotent at the application, and the only thing you give up versus true exactly-once is some database load for the dedup table and a small window where a duplicate could arrive after the dedup row has been pruned.
When exactly-once is worth the complexity
- Kafka-to-Kafka stream processing.
processing.guarantee=exactly_once_v2in Kafka Streams gets you transactional aggregation with no extra code. - Cross-topic atomic publishes. Producing to topic A and topic B in one transaction so a consumer reading both does not see a partial result.
- Pulsar transactions for multi-topic, multi-subscription consistency. Useful in transactional event-sourcing patterns.
When it is not worth it:
- Any consumer with an external side effect (database, API, file system, email). You will need idempotency at the consumer anyway, which makes the broker-side guarantee redundant.
- High-throughput pipelines where the transaction overhead (extra round-trips, transactional commits) measurably reduces throughput.
Operational notes
- Dedup windows have to be sized — Kafka
transactional.id.expiration.msdefaults to 7 days; JetStream--dupe-windowdefaults to 2 minutes. The window must exceed your worst-case producer retry interval or dedup silently fails. - Idempotent consumers depend on a deterministic message ID; if your producer logic generates IDs non-deterministically (timestamps, UUIDv4 at send time, etc.), you have at-least-once without useful dedup.
- A
processed_messagestable is itself unbounded; partition it by date and drop old partitions, or use a TTL-based store like Redis withSETNX+ EXPIRE. - “Effectively-once” is a real, valid architectural answer; do not let marketing copy convince a team that exactly-once is free.
Stack Harbor helps clients pick the right delivery-guarantee posture for each consumer in their pipeline — at-least-once with idempotent consumers as the default, transactional patterns where the broker supports them and the side effects allow, and at-most-once for telemetry. This is part of the design work we do as we onboard a messaging platform under our Managed Operations practice.