The outbox that shipped two packages
Failure story 04, in requests, retries and side effects.
The challenge
Section titled “The challenge”The order and its
OrderPlacedevent committed in one transaction, through a transactional outbox. How did one order create two shipments?
The setup
Section titled “The setup”| Assumption | Value |
|---|---|
| Order service | Writes the order and an OrderPlaced row to its outbox table in one transaction |
| Relay | Publishes outbox rows to a broker, at least once |
| Fulfillment worker | Consumes OrderPlaced, skips event IDs it has already processed, calls the carrier’s create shipment API, then records the event ID as processed and acknowledges the message |
| Carrier API | Creates a new shipment for every call |
What happened
Section titled “What happened”| Step | What happens |
|---|---|
| 1 | Order O-12 and event E-12 commit together. So far, the outbox is doing its job. |
| 2 | The relay publishes E-12. The worker receives it; E-12 isn’t in its processed table. |
| 3 | The worker calls the carrier. The carrier creates shipment S-501 and returns. |
| 4 | Before the worker records E-12 as processed, its container is stopped by a deployment. |
| 5 | The broker never got an acknowledgement, so it redelivers E-12 to another worker. |
| 6 | That worker checks its processed table: no E-12. It calls the carrier, which creates shipment S-502. |
| 7 | Two packages leave the warehouse. |
Why it went wrong
Section titled “Why it went wrong”The outbox guarantees one thing: the order and the event are saved together, so the event is never lost and never invented. It says nothing about how many times the event is delivered, and the relay is explicitly allowed to publish it more than once.
The worker’s deduplication table was correct, but it lived in the worker’s database. The side effect happened at the carrier. Between “the carrier did it” and “we wrote down that the carrier did it” there’s a gap that no local transaction can close, because the carrier isn’t in the transaction. A crash in that gap means the next attempt can’t tell whether the shipment exists.
Two designs
Section titled “Two designs”| Aspect | Design A: local deduplication | Design B: a fulfillment ID the carrier honors |
|---|---|---|
| What makes a repeat safe | The worker’s processed-events table | A stable ID, such as ship-O-12, sent to the carrier on every call |
| Crash after the carrier call | Repeat creates a second shipment | Repeat returns the existing shipment S-501 |
| Depends on | Nothing outside the worker | The carrier supporting idempotency keys, or a lookup by your reference |
| Where the guarantee stops | At the worker’s database | At the carrier, where the side effect actually happens |
The ID is derived from the business fact, one shipment per order, not from the event or the attempt, so every retry, redelivery and replay produces the same ID.
If the carrier has no idempotency key but lets you look up a shipment by your reference, the worker can check before creating: “is there a shipment with reference ship-O-12?” That narrows the gap without closing it, because two workers can check at the same moment. Pair it with a lock or a single consumer per order.
The check
Section titled “The check”- Count at the carrier. Inject a crash after the carrier call and before the worker records success, then let the broker redeliver. Expected: the carrier’s sandbox shows one shipment for
O-12. Counting rows in your own database proves nothing here. - Replay. Republish every
OrderPlacedevent from yesterday. Expected: no new shipments.
The variant worth arguing about
Section titled “The variant worth arguing about”The carrier supports neither idempotency keys nor lookup by your reference. What guarantee can the fulfillment service honestly offer?
It can choose at most once: record “attempting O-12” before the call, and never call again automatically if a previous attempt’s outcome is unknown. Unknown outcomes go to a person, who checks the carrier’s portal. Or it can choose at least once: retry, and accept occasional duplicates that are caught by reconciling the carrier’s daily manifest against orders. Either is honest. Claiming exactly once isn’t.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- The downstream action is itself idempotent, for example set order status to shipped.
- The side effect lives in the same database as the deduplication table, so recording it and doing it are one transaction.
- Duplicates are cheap and detected quickly: an extra notification, not an extra package.
For educators
Section titled “For educators”Discussion question. A colleague says, “We use the outbox pattern, so we have exactly-once delivery.” What exactly is true in that sentence, and what would you ask them next?
Exercise. The worker sends ship-<eventId> as the carrier’s idempotency key instead of ship-<orderId>. An operator later republishes O-12’s event under a new event ID while repairing the outbox. What happens?
Worked answer
The carrier sees a new key, ship-E-12b, and creates a second shipment. An event ID identifies a message; a republished message for the same fact gets a new ID. The idempotency key has to come from the business fact that must happen once: one shipment per order, or per order and parcel if orders can ship in several parcels. Then every path that produces the fact, including manual repair, produces the same key.
Further reading
Section titled “Further reading”- Chris Richardson’s description of the transactional outbox pattern (opens in a new tab), which notes that the relay might publish a message more than once.
- Amazon Builders’ Library: Making retries safe with idempotent APIs (opens in a new tab).
Related pages
Section titled “Related pages”- Example Lending, ADR-008: publish events through a transactional outbox
- Messaging, on at-least-once delivery
- Idempotency
Glossary: transactional outbox, at-least-once delivery
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.