Case study · Retail · complexity 4/5
How an online store sells every unit once and charges every customer once — from one monolith on one Postgres, through flash sales, to an order saga across services
This is an educational reference architecture for an online store, not the design of any one company. Where we cite a company's own published material (Amazon's Dynamo paper, Shopify's engineering blog and talks, Stripe's documentation), it is marked as documented.
Priya and Rahul press Buy at the same moment. Both requests read stock = 1, both decide the pair is free, and both create an order. One pair, two orders.
Architecture evolution
One Rails- or Django-style application serves the catalogue, carts, checkout and orders from one PostgreSQL database. Checkout takes stock with a conditional UPDATE in a short transaction, then authorizes the card through a payment provider with an idempotency key.
Components
What breaks next
Correctness, not load. Code that reads the stock and then writes it lets two shoppers buy the last unit, and a payment call retried without a key can charge a card twice.
Request A
Priya buys
Database
tickets: stock = 1
Request B
Rahul buys
Swipe the diagram sideways to see every column →
Where the time goes
4 ms total
One seat is left (stock = 1). Priya and Rahul press Buy at the same moment, so two requests run the same code: read the stock, and if it is above zero, write stock − 1 and create an order. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
Online retailers have published unusually detailed accounts of what goes wrong around checkout. Amazon's 2007 Dynamo paper explains why a shopping cart must accept writes even during failures, and Shopify's engineers have written about flash sales, waiting rooms, pods and inventory reservations.
Three documented sources, three rules
Checkout changes two scarce things at once: a unit of stock and the customer's money. It must change each exactly once, even when thousands of shoppers want the same unit and the network fails halfway through.
Six ways a naive checkout goes wrong
Catalogue, cart, inventory, checkout and orders look like one app, but they need different guarantees. The catalogue may be a little stale, the cart must always accept a click, and inventory, payments and orders must be exact.
Loose where it is safe, strict where it matters
A new store can run as one , a Rails- or Django-style application, on one PostgreSQL database. Checkout takes stock with one short, conditional write and creates the order, and the card is charged outside any database transaction. This is our educational design.
Stage 1: one app, one database
There are two common ways to stop two buyers taking the last unit. Either lock the row, check it and write, or make the check part of a single UPDATE. Both are correct in PostgreSQL; the difference is how long the row stays locked.
Same row, two ways to take a unit
Three ways to cool a hot row
Every store chooses when a unit stops being for sale: at add-to-cart, when payment starts, or after payment succeeds. Earlier means fewer oversold orders but more stock held by people who never pay. A reservation that expires is the usual middle ground.
The life of a reservation
A flash sale squeezes a busy day into a minute. Product pages are hammered, every checkout fights over one inventory row, and the database that serves everything else melts. Shopify has documented each of these effects; the numbers below are illustrative.
10:00:00, the drop goes live
The Stage 2 store keeps the same monolith and database, but puts a CDN and a waiting room in front of them and keeps every write to stock short. Shopify documented its own waiting room; the overall layout is our educational design.
Stage 2: protect the database from the crowd
A card payment can be split into an authorization, which holds the money, and a capture, which takes it. Every call that moves money carries an idempotency key, so a retry after a timeout never charges the customer twice.
Authorize now, capture when it ships
When inventory, payments and orders move into separate services with their own databases, no single transaction covers checkout. An order saga runs checkout as a chain of local transactions, each with a compensation that undoes it if a later step fails.
Stage 3: an orchestrator drives the order saga
When a step fails, undo the ones before it
The outbox turns a dual write into one commit
Once inventory lives in its own service, product pages and search learn about stock changes from events, seconds later. The page can say "in stock" after the last unit sold; that is acceptable only because the reservation step, not the page, decides.
Two answers to "is it in stock?"
The ideas a modern checkout uses were published over four decades: sagas in 1987, the always-writable cart in 2007, waiting rooms and idempotency keys in 2017, pods in 2018 and row-per-unit reservations in 2026.
From sagas to row-per-unit reservations
At Stage 3, browsing is served from caches and read models that may lag, while buying passes through a waiting room into a short, strict path of services joined by an order saga. This is a typical educational design, not any company's system.
Loose browsing, strict buying
Every protection at checkout costs something: locks cost throughput, holds cost sales, manual capture costs an extra step, and services cost a saga's worth of failure handling. The dials are set per product and per sale, not once for the store.
Five dials for a checkout
On a bad day, a payment call times out, a hold expires mid-payment, an event arrives twice, the inventory service is slow during a drop, and a compensation cannot run. Each needs a response designed before it happens.
A bad day for checkout, five ways
Four simulations show the behaviors this case study depends on: selling the last unit twice, a cache that empties at the worst moment, producers that outrun their consumers, and an order saga across services. All timings and loads are illustrative models.
Request A
Priya buys
Database
tickets: stock = 1
Request B
Rahul buys
Swipe the diagram sideways to see every column →
Where the time goes
4 ms total
One seat is left (stock = 1). Priya and Rahul press Buy at the same moment, so two requests run the same code: read the stock, and if it is above zero, write stock − 1 and create an order. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
Served
0/s
Errors
0%
p50 latency
0 ms
p95 latency
0 ms
Cache hit
0%
DB busy
0%
A warm cache shields a database that could never handle the full load on its own. Then every popular key expires at once.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
Queue depth
0
Oldest message age
0 ms
Rejected/s
0/s
Processed/s
0/s
Publish p95
0 ms
Producer wait
0 ms
Bounded and lossless
At 1,500 jobs/s or more, keep the oldest job under 5 s and reject nothing for 20 seconds.
An order service publishes 1,500 invoice jobs per second, and 4 workers can finish about 1,000 per second. The queue has no limit. Watch what grows. All numbers are an illustrative model.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
Order service
orchestrator
Payment service
charges cards
Inventory service
kettles: stock
Shipping service
books couriers
Swipe the diagram sideways to see every column →
Where the time goes
264 ms total
Order #42 is one electric kettle for ₹1,200. Orders, payments, stock and shipping are separate services, each with its own database, so no single transaction can cover all of them. The order service runs the steps as an orchestrator. Red steps are failures and the compensations that undo them. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.
The always-writable cart, Shopify's flash-sale defenses, pods and reservations, Stripe's idempotency and holds, the saga and outbox patterns and PostgreSQL's locking behavior are documented in the sources. The three stages, all traffic numbers, the reservation length and the saga's exact steps are our educational assumptions.
Where the evidence stops