Services

Resources

Company

/

Exactly-once semantics

Exactly-once semantics

Exactly-once semantics is a guarantee that each message changes the final result once, even if failures cause it to be sent or read more than once. Networks can always lose an acknowledgement, so systems do not deliver each message once. They deliver at least once and then remove the effect of duplicates, through deduplication, idempotency or transactions.

How it works

In Apache Kafka, an idempotent producer (enable.idempotence=true) tags writes with a producer ID and sequence number, so the broker drops duplicate retries. Transactions then commit output records and consumer offsets together, and readers with isolation.level=read_committed see only committed data. Without this, a consumer that auto-commits offsets every 5 seconds at 2,000 messages per second can reprocess up to 10,000 messages after a crash.

Why it matters in production

Duplicates turn into double counts in metrics, double charges and repeated emails. Kafka transactions only cover reads and writes inside Kafka. Any external side effect, such as a database write or a payment call, still needs an idempotent write keyed on a unique event ID.

See also

On the One2N blog

Further reading

A Kafka partition log explains exactly-once semantics. A consumer commits its offset, then crashes 5 s later. On restart it goes back to the last commit, so the messages in between, drawn in red, are read twice: 5 s × 2,000 msg/s = 10,000 messages. Below, the same event arriving again goes through a write keyed on a unique event ID, and the result is still one row.

Putting this into practice?

Talk to One2N about our backend engineering services

Talk to One2N about our backend engineering services