Many customers try to buy the last few units of the same product.
Prevent overselling with atomic state transitions, explicit expiry, and reconciliation for uncertain outcomes.
01. Write the invariant first
For each stock pool, confirmed sales plus active reservations must not exceed available inventory under the chosen accounting model. Decide whether stock is pooled globally or per warehouse, and when physical adjustments enter the ledger. A cached availability number is useful for browsing, not sufficient authority to reserve.
Represent a reservation with an ID, item, quantity, expiry, customer or order reference, and state. Use states such as active, confirmed, released, and expired, with allowed transitions rather than unrelated boolean flags.
02. Make check and decrement atomic
If one unit remains, two clients cannot both read one and independently decrement. Use a transactional conditional update that succeeds only when sufficient stock remains, together with reservation creation in the same transaction. Enforce a unique request key so a timeout retry returns the original reservation.
For an illustrative pool of ten units, reserving three leaves seven. Retrying the same request must still leave seven. A distinct request for eight must fail. Include the requested parameters in idempotency validation so one key cannot silently represent two different quantities.
03. Resolve confirmation versus expiry
A confirmation and an expiry worker may race on the same reservation. Both must conditionally transition from active, so only one wins. Evaluate expiry using an authoritative clock and explicit comparison semantics. Do not rely solely on an asynchronous cleanup job to determine whether a hold is valid.
Payment and stock usually live in separate systems. Coordinate them with explicit workflow states, idempotent provider calls, and compensating actions where needed. A late payment success after reservation expiry requires a declared policy, such as reacquiring stock or reversing the payment, rather than silently overselling.
04. Scale without losing the guarantee
Popular items concentrate writes on one stock pool. Partitioning by item spreads ordinary traffic but does not solve the hottest item. You can allocate bounded stock budgets to regions or warehouses, accepting reduced flexibility while preserving the total allocation limit.
Follow-up: a region cannot contact the stock owner. If strict no-oversell is required, reject or defer new reservations beyond an already allocated budget. Reconciliation detects mismatches among reservations, orders, and physical adjustments; it complements atomic admission rather than replacing it.
Practice the follow-up
Trace a confirmation arriving at the same moment as expiry. Which transaction wins, and what does the loser return?
Connect this to cache consistency.
Sources & further reading
PostgreSQL: transaction isolation. The interview scenario, calculations, and proposed design choices are illustrative.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.