Blog
At-Least-Once Delivery Means Duplicates: Design The Consumer First
A consumer updates the database, then crashes before acknowledging the message. The broker delivers it again. Nothing is broken; at-least-once delivery is keeping its promise.
Your consumer must keep its promise too.
Record Message Identity Atomically
When the business update and deduplication record share a database, commit them together.
insert into processed_messages(consumer, message_id)
values (:consumer, :message_id)
on conflict do nothing;
Only perform the business change when this insert claims a new ID, inside the same transaction. An in-memory cache does not survive restart and cannot arbitrate replicas.
Idempotent State Transitions Are Better
Sometimes the natural operation is repeatable: set order status to a specific version, upsert a snapshot if newer, or create a row under a unique event key.
“Increment balance by 10” is not naturally idempotent. It needs event identity or a ledger entry constraint.
Poison Messages Need An Ending
Retrying malformed data forever blocks a partition and burns resources. Define attempt limits, dead-letter or quarantine behavior, alerting, and replay tooling.
Acknowledgement timing, transaction boundary, and broker semantics must be tested together. A unit test that invokes the handler once does not cover delivery.
Duplicates are not rare edge case. They are part of the protocol. Put them in first test, not last incident.