Deduplication windows and idempotent consumers
Keys cannot be kept for ever
Section titled “Keys cannot be kept for ever”A server that remembers every key has to store every key. For a busy service that grows without end, and each lookup gets slower. So services remember keys for a limited time, the deduplication window, and forget them afterwards. A duplicate that arrives inside the window is recognised. One that arrives after it looks like a new operation, and is done again.
Two real services show how different the choice can be. Stripe says it may remove keys from the system automatically after they are at least 24 hours old, and that it generates a new request if a key is reused after the original is pruned. Amazon SQS FIFO queues use a 5-minute deduplication window: within it, only one message with a given deduplication ID is delivered, and any later message with the same ID is acknowledged but not delivered. The SQS documentation adds that it continues tracking the ID even after the message has been received and deleted.
A window is a promise with a time limit. It is sized by one question: how late can a duplicate arrive? A retry from a client arrives seconds later. A message stuck in a dead-letter queue and replayed by an operator can arrive days later, long outside a window of 5 minutes.
Mental model. The bank keeps cheque references for 90 days and then shreds them. Present an old cheque on day 91 and it is paid as if it were new. Whether that matters depends on how long a cheque can honestly stay in someone’s drawer.
Four cases
Section titled “Four cases”| Case | Window | Duplicate arrives after | Result |
|---|---|---|---|
| Client retry to a payment API with keys | 24 hours or more | a few seconds | Recognised, the saved answer is replayed |
| Standard message redelivery after a lost acknowledgement | none in the queue | 5 seconds, the lab’s visibility timeout | Delivered again: the consumer must cope |
| FIFO queue, producer sends the same ID twice | 5 minutes | 2 minutes | Accepted but not delivered |
| FIFO queue, operator replays the same message | 5 minutes | 6 minutes | Delivered as a new message |
In the last row, the second delivery is not a bug in the queue. The window ended, as documented. A system that depends on the window to avoid double effects has an expiry date on its correctness.
The consumer is the last line
Section titled “The consumer is the last line”The window is a filter in front of the work. The consumer is where the work happens, and it can make duplicates harmless whatever the filter lets through. There are two ways.
Make the operation idempotent by nature. Setting a field to a value, storing a record under an id, or deleting a row can be repeated safely. “Set the status of order 42 to shipped” is the same after a second delivery. “Add 1 to the stock count” is not. When the operation can be written as a state to reach instead of a change to apply, do that: it needs no table of ids and has no window.
Record the message id with the effect. When the operation cannot be rewritten, the consumer keeps a table of the ids it has handled. For each message it checks the id, does the work, and records the id, and the check, the work and the record belong in one database transaction. If the record were saved separately, a crash between the work and the record would do the work again on redelivery, and a crash between the record and the work would lose it. The same rule applied to keys in the last lesson. The table can be pruned, and its window is then the same question again: how late can a duplicate arrive?
The outbox example of the last lesson ends with this consumer: it remembers the ids it has handled, receives 4 deliveries for 2 orders, and does the work 2 times.
Check your reading.
Check your understanding: deduplication windows
This quiz needs JavaScript to check your answers.
What happens if two consumers get the same message at once?
Section titled “What happens if two consumers get the same message at once?”A table of ids that is checked and then written has a gap in it. Two consumers can both check, both find nothing, and both do the work. The id column needs a unique constraint so that only one insert succeeds, and the consumer that loses must not do the work. A lock on the id does the same job. The sketch in the key lesson is single-threaded and does not show this, which is why its last comment says a real server needs one.
- A deduplication window is finite: a duplicate that arrives after it is done again.
- Stripe may remove keys at least 24 hours old, and SQS FIFO queues use a 5-minute window.
- The consumer should be safe whatever the window does, by making the operation idempotent by nature or by recording ids with the effect.
- The check, the work and the record belong in one transaction, with a unique constraint for concurrent duplicates.
That is the end of the lessons. The next page gathers them: a recap, a checklist and a quiz.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.