CAPSTONE: CHECKOUT UNDER FAILURE
Keep taking orders while the payment provider is on fire
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.
// 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' };const deadline = Date.now() + 1000; // the client gives up at 1 s
const existing = await orders.get(orderId);
if (existing) return existing; // idempotent resubmit
const items = await cart.get(orderId);
await inventory.reserve(items);
const key = `order-${orderId}`; // same key on every attempt
let result = { status: 'queued' };
if (breaker.allows()) {
// retry with backoff + jitter, same key, only while the 1 s deadline has room
try { await retryWithin(deadline, () => withTimeout(payment.charge({ amount, key }), 250)); breaker.record(true); result = { status: 'paid' }; }
catch (e) { breaker.record(false); }
}
if (result.status === 'queued') await queue.push({ orderId, key });
await orders.save(orderId, result);
return result; 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.
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
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" }.
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.
“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.
USED IN
Chapters that build on this lesson.
- On-call and incidents for managersEngineering Manager