Project 07
Inventory reservation
Concurrency and consistency
Brief
The inventory service behind Projects 6 and 16: track stock per SKU, reserve on checkout, confirm on payment, release on timeout or cancellation — correct under heavy concurrency.
Targets
5k reservation attempts/sec on hot SKUs · zero oversells · reservation p99 < 100 ms · expired reservations released within 30 s.
Must cover
- Overselling prevention — proven under concurrent load (→ 71).
- Reservation expiry with leases (→ 137).
- Stock reconciliation — periodic comparison against movements, with correction entries (→ 81).
Design challenges
- The core race (→ 71): 100 concurrent buyers, 3 units left. Solve three ways and
benchmark: atomic conditional update (
UPDATE … WHERE available >= n),SELECT … FOR UPDATE, optimistic versioning with retry. - Reservation expiry (→ 137): holds as leases; the race between expiry and payment confirmation handled explicitly.
- Hot-SKU contention: the single-row serialization limit — when sharded counters or slotting become necessary, and their complexity cost (→ 88, 73).
- Idempotent reserve, confirm, and release operations for safe retries (→ 38, 116).
- A consistency statement per read (→ 129): what the "in stock" badge guarantees vs. the reservation itself — and where stale reads are acceptable.
- An audit trail of every stock movement feeding reconciliation (→ 81); evaluate the event-sourced variant honestly and adopt or reject it with reasons (→ 120).
Out of scope
Multi-warehouse allocation and demand forecasting.