CAPSTONE: CHECKOUT UNDER FAILURE

Keep taking orders while the payment provider is on fire

L320 MINRELIABILITY
PREDICT

The provider accepts requests but never responds. A naive checkout calls it with no timeout, from one shared pool of 4 workers that also serves cart lookups, and sends a new idempotency key on every attempt. It sees 4 checkouts, one of them a customer resubmitting order #2. What happens to the next cart lookup, and how many charges does order #2 have?

Checkout is cart → inventory → payment → order record. The naive version calls payment with no timeout from a shared worker pool and sends a fresh key on every attempt. One slow dependency then parks every worker, and every resubmit is a new charge. The resilient version composes the patterns from this path: a timeout on payment, bounded retries with jitter, a circuit breaker, a bulkhead so cart and inventory never share workers with payment, and a degraded mode that accepts the order and queues the payment under one idempotency key per order.

▶ SIMULATION
WORKER POOL · shared
free: 4 / 4
stuck on payment (no timeout): 0
browse ok 0 · failed 0
PAYMENT PROVIDER
status: responding
charges by order: —
t = 0 ms
BREAKER + QUEUE · not in use
breaker: CLOSED (0 failures, opens at 5)
queued payments: 0
settled from queue: 0
ORDERS—
LAST EVENTwaiting
✓ cart and inventory requests keep succeeding during the payment outage✓ no order is charged twice
SCHEDULE
01[SHOPPERS]5 shoppers browse: cart + inventory lookups
02[SHOPPERS]shopper 1 checks out order #1
03[PROVIDER]provider stops responding (requests land, responses hang)
04[SHOPPERS]shopper 2 checks out order #2
05[SHOPPERS]shopper 2 resubmits order #2 (no confirmation seen in 1 s, or Pay pressed again)
06[SHOPPERS]shopper 3 checks out order #3
07[SHOPPERS]shopper 4 checks out order #4
08[SHOPPERS]5 shoppers browse: cart + inventory lookups
09[SHOPPERS]shopper 5 checks out order #5
10[PROVIDER]provider recovers: hung responses finally return
11[QUEUE]queue worker tick (every 2 s): retry queued payments
12[SHOPPERS]5 shoppers browse: cart + inventory lookups
Start of naive checkout scenario. 12 steps.
// naive: shared pool, no timeout, a new key every attempt
const items = await cart.get(orderId);
await inventory.reserve(items);
await payment.charge({ amount, key: uuid() });   // hangs forever during the outage
return { status: 'paid' };

Watch both invariants. In NAIVE CHECKOUT the browse invariant turns red at the second browse step and the double-charge invariant turns red the moment shopper 2 resubmits. In FIX: RESILIENT CHECKOUT the same steps keep browsing green, the breaker opens after 5 failed calls, and the queue worker settles every order exactly once after the provider recovers. Drag WORKERS down to 2 and up to 8 in NAIVE CHECKOUT: with 8 workers the pool survives and only the double charge is left. The resilient run does not change with WORKERS. In this model each step finishes before the next, so once payment has a timeout it never holds a worker for long; the bulkhead split is shown, not stressed. Each payment attempt also gets only what is left of the shopper’s 1 s deadline, so checkout #2 tries twice and then queues instead of retrying past the point where the shopper gave up.

INJECT A FAULT NOT YET

The naive checkout, one shared worker pool, a healthy provider. Start the provider outage at the step where it does the most damage: browsing fails, or an order is charged twice.

GOAL: a browse request fails or an order is charged twice

FIX IT NOT YET

Rewrite placeOrder so checkout keeps accepting orders during a payment outage, never calls the provider while the breaker is open, and never charges an order twice, even when the client resubmits it. Return { status: "paid" } or { status: "queued" }.

Mental model:

A slow dependency is more dangerous than a dead one. Bound every call (timeout), bound every retry, and isolate the slow path so it cannot consume what the fast paths need. Then decide what the business wants when the dependency is gone: usually accept and settle later, which is only safe if every attempt for the same order carries the same idempotency key, and only sound if you hold fulfilment until the charge settles and have a plan for the ones that are declined. The patterns are not a menu; a checkout needs all of them, each covering a hole the others leave.

IN PRODUCTION

Any flow with a third-party call in the critical path: payments, SMS and email providers, identity providers, shipping-rate APIs, fraud scoring. The symptom is a whole service going down for a dependency that only one endpoint needed, followed by duplicate side effects when it recovers.

IN INTERVIEWS

“Design checkout so a payment outage does not take the store down.” Walk the request path, put a timeout and a breaker on payment, isolate it with a bulkhead, accept the order and queue the payment, and explain why the idempotency key is what makes the retry, and the queue, safe.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.