Idempotency and deduplication
At-least-once delivery means every consumer will one day see the same message twice. This guide is the consumer's side of Idempotency keys and safe retries: making a repeat harmless, with an action that can't double up, a version number, or a record of the messages already handled. Interviewers ask where the message ID comes from, where you record it, and for how long.
The thermostat and the guest list
"Set the heating to 21 degrees" can be said three times, and the room ends up at 21. "Turn it up 2 degrees" said three times makes the room 6 degrees warmer. When an instruction can't be phrased the first way, you need a list instead. The doorman at a party ticks each name as the guest arrives, so someone who walks round the block and queues again is recognised. The list is for tonight only; next week's party starts a new one.
- "set to 21" → a naturally idempotent action: set a value, upsert a row, delete
- "turn it up 2" → an action that isn't: increment, append, send an email, charge a card
- the doorman → the consumer, and the guest queueing again → a redelivery
- the guest list → a dedup store of processed message IDs
- tonight only → the dedup window: how long IDs are kept