What it really takes for an effect to happen once: idempotence, transactions and dedupe working together.
Exactly-once processing means that the effect of a message (a charge, a row, a count) happens one time, even when the message is delivered more than once. True exactly-once delivery over a network is impossible, so we build it from at-least-once delivery plus safe repeats.
A message system can fail in two places: the send (the ack is lost, so the sender sends again) and the receive (the consumer does the work, then crashes before it records that it did). In both cases the safe choice is to try again, because a lost message is worse than a repeated one. That gives at-least-once delivery.
The reason we cannot have "exactly once" on the wire is the Two Generals problem. A sender can never be sure whether a missing ack means "the message was lost" or "the message arrived and only the ack was lost". It must choose between risking a loss and risking a repeat.
So the goal moves from delivery to processing. We accept repeats, and make sure applying the same message twice has the same effect as applying it once. This is effectively-once (or "exactly-once semantics"). The two ingredients are always the same: at-least-once delivery and idempotent or atomic processing.
| Failure | What happens | Result |
|---|---|---|
| Producer ack lost | The producer sends the same message again | Two copies in the log |
| Consumer crashes after the work, before the ack or offset | The message is redelivered after restart | The work is done twice |
| Consumer is slow, and the broker redelivers | Two consumers hold the same message | Parallel duplicate work |
| Network retries by a client or proxy | The same request arrives twice | A duplicate write |
| Replay by an operator | Old events are processed again on purpose | Everything is repeated |
| Tool | How it works | Works when |
|---|---|---|
| Idempotency key | Each action carries a unique id. The receiver stores it, and ignores a repeat | The effect is in a store you control |
| Atomic offset and result | The consumer writes its output and its read position in one transaction. Both commit or both roll back | Input, output and offsets live in the same system |
| Kafka transactions (EOS) | An idempotent producer plus transactional writes to output topics and offsets. Readers use read_committed | Kafka to Kafka flows |
| Flink two-phase commit sink | A checkpoint marks a consistent point. The sink pre-commits, then commits after the checkpoint succeeds | The sink supports transactions or idempotent writes |
| Inbox table | Save the message id and the effect in one local transaction | The effect is a database write |
| Outbox table | Save the change and the outgoing event in one transaction. A relay publishes it, possibly more than once | You need to publish events reliably |
A simplified model with one fixed failure pattern (no random numbers). The log lines show only the unusual events.
BEGIN; INSERT INTO processed(id) VALUES (:id) ON CONFLICT DO NOTHING; if inserted: apply effect; COMMIT; ack| Term | Meaning in easy words |
|---|---|
| Exactly-once processing | The effect of each message happens one time, even if it is delivered more than once |
| At-least-once | A message is retried until acked, so it may arrive more than once |
| Effectively-once | Another name for exactly-once effect: at-least-once plus safe repeats |
| Idempotency key | A unique id that lets a receiver recognise a repeat |
| Idempotent producer | A producer whose resends are dropped by the broker using sequence numbers |
| Offset | The reader's position in a log. Saving it marks the work as done |
| Transactional write | Output and offset saved together, so both happen or neither |
| read_committed | A reader mode that ignores data from unfinished transactions |
| Checkpoint | A consistent snapshot of a stream job (as in Flink) that it can restart from |
| Two-phase commit sink | A sink that pre-commits, then commits after the checkpoint succeeds |
| Inbox / Outbox | Tables that remember received messages and queue messages to publish |
| Zombie producer | An old instance that still writes after a new one took over. Epochs or fencing stop it |
| Choice | We gain | We lose |
|---|---|---|
| Dedupe table | Works with any broker | Storage, and a lookup for each message |
| Kafka transactions | Atomic read, process, write | Lower throughput, Kafka-only boundary |
| Short id retention | Small table | A late duplicate may slip through |
| Long id retention | Catches late repeats | More storage and cleanup |
| Natural idempotent write (upsert, set) | No extra table | Not every effect can be an upsert |
| At-most-once instead | Lowest cost | Lost data |