Architecture / 5 / 9
Transactional outbox
A shop saves a purchase in its database and then publishes an event so the loyalty service can credit points. Two writes to two systems with no shared transaction: now and then the commit succeeds and the publish does not, and the points are simply gone. The outbox writes the event into the same database in the same transaction, a relay delivers it at least once, the consumer drops duplicates, and keys, cleanup and log tailing deal with the rest.
- 14 min
- Advanced
- 12 steps
- Lost events
- 0 /day
- Duplicate credits
- 0 /day
- Out-of-order events
- 0 /day
- Commit to publish
- 4 ms
- Unsent outbox rows
- 0
- Rows in the outbox
- 0
- request
- reply
- redelivery
- duplicate
- lost
- call: the reply returns on the same line
- async: no waiting
Numbers are a model scenario, not measurements
The real request
Purchase service → BrokerRequest
POST /v1/purchases -> purchase p_9051
1 BEGIN
2 INSERT INTO purchases (id, user_id, total_minor) ...
3 COMMIT ok 3 ms1
4 broker.publish("purchases", PurchaseCompleted) ok 4 ms2
-- two writes, two systems, no shared transaction3
COMMIT— The purchase is durable. From here on, it happened.publish— A second, separate write over the network. It can fail on its own.two systems— The database and the broker cannot share one transaction.
Build it yourselfCheat sheetCompare lessons
Course: 74 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes
11 Case studies