Technical Debt — High-Concurrency Stock Contention Scaling Gap
By Engineering Team•October 8, 2026•4 min read
| Metadata | Details |
|---|---|
| System | inventory-service — StockBalanceService |
| Document ID | TD-2026-INV-001 |
| Author | Engineering Team |
| Status | Tracked |
SR-019’s FR-11 and ADR-00035’s Part 3 link here rather than restating this gap inline; this is the one place it’s tracked.
TD-01 — No pre-database fast-reject layer for large-scale simultaneous stock contention
- Status: Open (deliberately deferred, not unplanned — see ADR-00035 Part 3)
- Severity: Low today; escalates to High ahead of any specific scheduled high-demand event (flash sale, limited restock) that the business chooses to run
- Affected component:
inventory-service’sStockBalanceService.ReserveInventoryand theinventory_balancesconditional-update/row-lock mechanisms it relies on (stock_balance_reservation_impl_svc.go,stock_balance_write_impl_svc.go) - Evidence:
reserveInventoryLine’s conditionalUPDATE ... WHERE quantity_available >= $1andProcessWarehouseStockBalance’sINSERT ... ON CONFLICT (warehouse_id, sku_id) DO UPDATE ... RETURNINGare the only two concurrency-control mechanisms in the reserve/consume path (see ADR-00035 Parts 1–2). Both are correct under concurrent access, but both resolve contention at the database row itself — there is no layer upstream of PostgreSQL that pre-filters, rate-limits, or fairly sequences a large burst of simultaneous requests against the same SKU before they reach the database. No Redis (or equivalent in-memory store) client, token-bucket data structure, or429-style fast-reject response path exists anywhere ininventory-serviceororder-servicetoday. - Impact: Under ordinary day-to-day contention (a handful to a moderate number of simultaneous orders racing for the same SKU), the current mechanisms already satisfy BRD-018’s BR-01/BR-02/BR-03 in full. At a much larger, scheduled burst of simultaneous demand against one SKU, every request — including every request that is mathematically guaranteed to lose — still pays a full database round trip and queues on the same row lock, which is the exact scenario BRD-018 BR-03 (checkout stays fast under contention) and BR-05 (first-come-first-served fairness) are written to anticipate but which today’s mechanism has not been built, or load-tested, to satisfy at that scale. The business risks finding this limit live, during an actual flash-sale-style event, rather than ahead of one.
- Remediation direction: Build a Redis-backed, in-memory token-bucket layer per contended SKU, sitting in front of
ReserveInventory’s database call: each incoming reservation attempt atomically decrements the bucket (e.g. via a Lua script orDECR-with-floor) and is immediately rejected with a fast, clear “sold out” response (surfaced to the caller as an HTTP 429-equivalent) the instant the bucket is empty, without ever reaching PostgreSQL. Assign each accepted request a monotonically increasing sequence token to give a genuine first-come-first-served order of who gets to actually attempt the database reservation. Run a background reconciliation process that periodically re-syncs each bucket’s count againstinventory_balances.quantity_available, so the cache is a fast-path optimization layered on top of the database’s own guarantee (ADR-00035 Part 3, Option 1), never a second, independently-authoritative source of truth. Trigger this build ahead of a specific, business-identified high-concurrency event — per BRD-018 BR-04 — rather than building it speculatively with no event to validate it against. - Related requirement: SR-019 FR-11, BRD-018 BR-04/BR-05
Related Documents
- BRD-018 — Concurrent Demand: Stock Integrity Under Simultaneous Orders and the Scaling Decision
- SR-019 — Concurrent Demand: Stock Integrity Under Simultaneous Orders System Requirements
- ADR-00035 — Conditional-Update Reservation vs. Row-Locked Consumption, and the Redis/Token-Bucket Scaling Decision
- ADR-00026 — MongoDB TTL Cache vs. Redis for Checkout Session — the platform’s prior, analogous Redis-deferral decision
- Architecture — Inventory Stock Reservation, Consumption, and Concurrency Control — the two mechanisms this gap sits in front of
- Technical Debt — Checkout Completion Payment Flow Gaps — TD-05, the related downstream gap in
ConsumeInventoryActivity’s own saga-visible failure state - Microservices: inventory-service