Q10 · How do you design for exactly-once or effectively-once processing guarantees?
Back to 24 Architecture Questions.
The question is not a broker feature comparison. The axis it turns on: which side of the effect is deduplicated, the write or the delivery. Exactly-once delivery is not a thing anyone can sell you: retries, rebalances, and crashes make redelivery certain. Exactly-once effects are a thing you can build: at-least-once delivery, plus a guard in front of every write.
What the answer should contain
Claim the honest name first. Effectively-once: at-least-once delivery plus an idempotent effect. Anyone claiming exactly-once delivery is describing a closed system without failures, retries, or clocks.
Deduplicate by request, or by state. By request: a key per operation, stored with its outcome, for operations triggered from outside. By state: conditional writes (“apply only if the source offset is not yet processed”), for stream processing where the key is the position in the log.
The effect and the marker commit together. “Already done” and “done” must land in one atomic write, or a crash between them reintroduces the duplicate. In stream processing this means committing the consumer offset with the output, in the same transaction, so a replay recognizes its own work.
Fence the zombies. Every guard above assumes one active writer per key. A rebalance can make two: a consumer stalls in a long GC pause, its partition is reassigned, it wakes, and writes its result as well. The key store cannot tell which of the two is current, so each writer carries a fencing token (a generation number) and the store rejects any write from a superseded generation.
Duplicates are not the only repeat. Out-of-order arrival is the sibling problem: the guard plus a per-key sequence or version decides whether a late, older event still applies.
Test by killing processors mid-batch. Assert the effect count after recovery. If the test suite never crashes anything, it has never tested the guarantee.
Ideas worth mentioning out loud
- “Delivery is a fact of the transport; deduplication is a property of the write. Conflating them is how teams buy broker features instead of designing guards.”
- The broker’s “exactly-once API” is exactly this pattern, industrialized: transactional offsets plus fenced producers. It is worth using, and worth knowing it is not magic.
- The cost of the guarantee is a serialization point (the key store, the fencing store). Give it a timeout, a retry cap, and its own pool, like any other dependency on the path.
Follow-ups to expect
- Where does the guard live? In the consumer, as close to the write as possible. A dedupe cache in front of the effect dies with the process; a store behind the write survives it.
- What about two processors racing? The unique constraint or the fencing token arbitrates: one write wins, the other reads the stored outcome.
- When is at-least-once enough without any guard? When the effect is naturally idempotent: a state-setting write, a delete by stable ID.
Red flags in an answer
- “Our broker guarantees exactly-once”, with no mention of the effect side.
- Deduplication in an in-memory cache, or on the consumer side only.
- No fencing: the zombie-writer double-write is left to luck.
- Offsets committed separately from the output they produced.
Sources
- You Cannot Have Exactly-Once Delivery, Tyler Treat: the delivery-side half of the axis. Redelivery is how transports survive failures, so the guard belongs to the write.
- Exactly-once Semantics is Possible: Here’s How Apache Kafka Does It, Confluent: the effect-side half. Transactional offsets and fenced producers are this answer’s guard and zombie fencing, industrialized, and the post is honest that it is exactly that and not magic delivery.