Idempotent Consumers and Outboxes
In this article
- Explain why a queue, a retry or a resumed run can repeat a write, and why repeating a read is harmless.
- Use a receipt keyed by the action id to skip a redelivered message.
- Trace an outbox and its relay through two crash points and say which one sends the provider call twice.
- Give a reversal its own action id so that it also runs once.
- Terms defined here: dedupe receipt, outbox pattern.
Overview
The shop's support agent must refund 49.00 EUR for order 1042, and the refund job is about to run a second time: the queue redelivers it after a worker's lease (a time-limited claim on the job) ran out, a caller retries, or a stopped run resumes and repeats its step. Queues work this way on purpose: they promise a message arrives, and may deliver it more than once. [1] How do you make sure the money leaves exactly once? With three pieces: a receipt that shows the job was already done, an outbox that makes "save to the database and call the payments provider" safe to repeat, and a separate identity for the undo action. Afterwards you can follow a refund through redeliveries and crashes and count exactly what the provider received.
Sign in to keep reading
The rest of this article opens with an account. An account also keeps your progress and quiz results on every device, and lets you follow news for up to five topics.
Still to read in this article
- Why a write runs twice
- One id per action
- The receipt
- The outbox
- Undo is a write too
- Common mistakes
- In production
- Sources
- Check yourself, 13 questions