Card 03/ 07

ExampleDifficulty: Intermediate2 min

Heuristics Example: A Flash Sale Checkout

A spike of writes, a payment provider that times out, and clients that retry.

The same reading, pressure then heuristic then pattern, works on a brief that looks nothing like a news site. This example reads a flash sale checkout, which is about writes, failures and retries rather than reads, and arrives at different patterns by the same steps.

The brief. A shop sells 5,000 discounted sneakers at noon. For ten minutes, checkout traffic is fifty times normal. Payments go through a third-party provider that sometimes takes 30 seconds and times out. Shoppers' apps retry a checkout that times out. No pair may be sold twice, and no shopper charged twice.

From pressure to pattern for the flash sale
Pressure in the briefHeuristicPattern
Fifty times normal orders in ten minutesA spike of writesA queue in front of order processing
Bots and refreshing shoppers hammering checkoutRequests past the limitRate limiting at the edge
A spike at a known time, in secondsLoad that rises faster than servers startScale ahead on a schedule
Apps retry, and the provider is slowRetries and safetyAn idempotency key on every order and payment
Exactly 5,000 pairsCorrectness of stockStock counted in a relational database transaction

The design that results. Rate limiting at the edge turns away excess requests. Servers, scaled ahead of noon, accept each order, reserve a pair in one database transaction, put a payment job on a queue, and reply "order received". Workers take the payments at the pace the provider can handle, each with the order's idempotency key, so a retry never charges twice.

What it shares with the news site. The method, and almost nothing else. There, the pressures were reads and distance, and the answers were at the edge and in the cache. Here, they are writes, failures and retries, and the answers are a queue, limits and keys. A CDN does nothing for the payment call; a cache does nothing for stock that must be exact.

So the reading transfers even when the patterns do not: find the pressures first, and let them choose the parts.