Idempotent Event Consumers for At-Least-Once Delivery
Many queues and event brokers provide at-least-once delivery: a message that has been accepted can be delivered again when acknowledgements are lost, consumers crash, visibility timeouts expire, or the broker retries after uncertain outcomes.
Duplicates are therefore not exceptional. A robust consumer should assume that the same logical event can arrive more than once.
Why duplicates happen Consider this sequence:
a consumer receives an event; it updates the database successfully; the process crashes before acknowledging the message; the broker makes the message visible again; another consumer receives it. The broker cannot know that the database update happened. Redelivery is the safer choice.