Design a Last-Unit Inventory Reservation Service
Coordinate an order service with authoritative inventory reservation so retries, races, and uncertain cross-service outcomes never oversell the last unit.
Problem statement
Design an inventory reservation service for a retail checkout path.
For this exercise, assume a shopper reserves one SKU unit while adding it to a cart and later confirms or abandons the order. For this exercise, assume two concurrent reservation requests cannot both acquire the last available unit.
For this exercise, assume 50,000 inventory mutation decisions per second across 20 million SKUs with in-region p99 under 30 ms.
A scale follow-up raises that same reserve, confirm, abandon, and expiry workload tenfold to 500,000 inventory mutation decisions per second and may intensify hot-SKU skew without weakening the no-oversell guarantee. For this exercise, assume every mutating request carries a stable operation identity.
Database deadlock victims and requests with ambiguous timeouts are retried with that same identity and must not move inventory twice. For this exercise, assume catalog authoring, pricing, payment processing, and last-mile delivery are out of scope.
For this exercise, assume a reservation expires after a short hold window and unused units return to available inventory. The deadline is authoritative: a confirmation at or after it fails even if asynchronous cleanup has not run.
After establishing the one-unit reservation foundation, a follow-up extends it to a request containing 1 to 20 unique SKU quantity lines.
Each line may be partially fulfilled from zero through its requested quantity, lines commit independently under their owning SKU authority, and retries with the same batch operation identity must return the original per-line result.
Add a service-boundary follow-up. For this exercise, assume an Order Service owns durable order intent and order lifecycle, while an Inventory Service alone owns SKU counts and reservations; the services do not share a database transaction. One logical order submission carries a stable identity across both services.
An order may become inventory-pending, reserved, rejected, cancelled, or outcome-unknown. If Inventory commits but the response is lost, Order must not claim rejection, mark the order fulfilled without proof, or issue a new logical reservation.
Design durable coordination, retry, status reconciliation, and compensation, and compare a saga built from local transactions with a distributed transaction protocol.
What to build
- A shopper can submit one durable order intent whose stable identity lets Order and Inventory services converge on one reservation of an available SKU unit; the reservation later expires if unused.
- The shopper can confirm a reservation into a sold unit or abandon it so the unit returns to available inventory.
- Two concurrent reservation requests cannot both acquire the last available unit; a valid unexpired reservation exclusively holds its unit.
- Clients can read current available and reserved counts for a SKU.
At scale
- For this exercise, assume 50,000 inventory mutation decisions per second across reserve, confirm, abandon, and expiry initially, followed by a tenfold-growth exercise at 500,000 inventory mutation decisions per second.
- For this exercise, assume 20 million SKUs with skewed demand, so unrelated SKU state must be sharded and a small set of hot SKUs must be isolated without splitting one stock invariant.
- For this exercise, target in-region p99 under 30 ms for a healthy reserve or confirm at both workload levels.
- For this exercise, the last remaining unit is the correctness target: exactly one of two simultaneous reserves may succeed, including across deadlock retries and ambiguous responses.