Sagas, stock holds and checkout that survives a sale
Updated Nov 10, 2026 · 4 min read
Checkout looks like one action to a shopper. Behind it are several, usually owned by different services. Reserve the stock, charge the card, book a courier, send a receipt. Each service has its own database, and any step can fail after the earlier ones succeeded. A sale makes it harder, because most of the day’s orders arrive in a few minutes. This article covers the three ideas that keep checkout correct under that pressure.
Why not one big transaction
Inside one database, a transaction makes several writes all-or-nothing. Across services there’s no such thing for free. Two-phase commit can do it in theory, but every service must take part and hold its locks until the slowest one answers. Outside providers, like a card network, won’t take part at all. So checkout needs a different way to stay consistent.
Sagas and compensating steps
A saga splits the work into local steps, each committed on its own, and gives every step a compensating step that undoes its effect. If step three fails, the saga runs the compensations for steps two and one, newest first. The shopper ends up as if they never checked out.
| Step | Compensating step |
|---|---|
| Reserve stock | Release the hold |
| Charge the card | Refund the charge |
| Book a courier | Cancel the booking |
| Email the receipt | None, so it runs last |
Three rules make sagas work in practice.
- Put steps that can’t be undone last. Once an email is read, nothing unsends it, so it goes after every step that might still fail.
- Make every step and every compensation safe to repeat. A saga that crashes and resumes will run some of them twice.
- Record progress durably. An orchestrator, a small service that drives the saga, writes down each finished step, so after a crash it knows where to pick up.
Holding stock without overselling
With 500 consoles and 10,000 shoppers, the timing of “this one is yours” decides everything. Take it when the item goes in the cart and abandoned carts lock stock away. Take it only once payment succeeds and hundreds of people pay for the last few units. The usual answer is a hold. Checkout takes the item with an expiry, often around 10 minutes, and the hold becomes a sale when payment succeeds.
UPDATE stock SET available = available - 1
WHERE sku = ? AND available > 0
-- one row changed means the hold is yours
-- no rows changed means sold outThe conditional update is atomic, so two shoppers can never both take the last unit. A sweeper returns expired holds to stock, so a shopper who walks away keeps nothing.
Retries that never order twice
Phones on weak signal time out, and an app can’t tell whether a timed-out request worked. So it must be safe to send again. The app makes an order id, an idempotency key, when checkout opens, and sends it with every try. The server stores it under a unique constraint. The first try creates the order. Any repeat finds the id and gets the same order back. Pass the same key to the payment provider and a retried charge is caught there too.
A queue to absorb the rush
Payment providers accept only so many charges a second. If orders arrive faster than that, the extra ones fail. A queue between checkout and payments turns those failures into a short wait. Checkout records the order, says “order received”, and workers charge cards as fast as the provider allows.
The arithmetic is simple. A queue grows by what arrives minus what leaves, every second. At 3,000 orders a second against a limit of 1,000, it grows by 2,000 a second, so after a 10-minute rush 1.2 million orders are waiting. At 1,000 a second that’s 20 minutes to charge them all, even if no new orders came in. Tell shoppers their order is confirmed when it’s charged, not before.
Reads, writes and what a cache can’t do
A cache helps product pages, which are the same for everyone. It doesn’t help writes. Every hold and every order lands on the database, however good the cache is. When a sale pushes writes up, the database needs real headroom. Move the reads that can be slightly stale, like showing stock on a product page, onto read replicas, and keep the writes and the hold itself on the primary.