Architecture / 3 / 4
Sagas and the outbox
One order needs a payment and a stock reservation in two other services, each with its own database. There is no single transaction to wrap it in. How to keep the three in agreement without locking the world.
- 15 min
- Advanced
- 11 steps
- Order p95
- 320 ms
- Charged, no order
- 0 /day
- Lost events
- 0 /day
- request
- reply
- redelivery
- duplicate
- lost
Numbers are a model scenario, not measurements
The real request
Shop app → OrdersRequest
POST /orders HTTP/1.1
Host: api.example.com
{
"customer_id": "u_42",
"items": 1[{ "sku": "lamp-07", "qty": 2 }],
"amount": 2500000
}
Response
HTTP/1.1 202 Accepted
{
"order_id": "1042",
"status": 3"pending"
}
items— What to reserve in Inventory.amount— What to charge in Payments, in the smallest currency unit.status—pending: the order exists, but payment and stock are not settled yet.
Course: 42 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes