Idempotent Consumers and Outboxes
In this article
- Explain why a redelivered message, a retry or a resumed run can repeat a write, and why repeating a read is harmless.
- Skip a redelivered message with a receipt keyed by the action id and written in the same transaction as the effect.
- Use an outbox and an idempotency key to keep a database update and an API call consistent, and say where a crash still sends the call twice.
- Give a compensation action its own id so that it also takes effect once.
- Terms defined here: dedupe receipt, outbox pattern.
Overview
Worker A in the shop's support agent made the tool call create_refund for order 1042, froze, and lost its lease. The queue handed the same job to worker B, and B is about to call create_refund too. The fencing token keeps A's late write out of the shop's database, but the payments provider has never heard of the shop's tokens. Unless something else stops it, the customer gets 49.00 EUR twice.
Three pieces stop it. A receipt lets a consumer see that a message was already handled. An outbox turns "update the database and call the provider" into one database write plus a call that is safe to repeat. And an undo action gets its own name, so that it too runs once. The shop, its order and its run are made up; the patterns are standard.
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 repeat hurts a write and not a read
- One name for the work
- The receipt
- The outbox
- Undo is a write too
- The whole run, in code
- In production
- Sources
- Check yourself, 13 questions