← Technical notes

Confidential / NDA

Cart and stock reservation

Both channels share one cart. Stock is per variant and limited: no overselling, and no stock locked by idle carts.

Background

Both channels share one cart. Stock is per variant and limited. The goal: no overselling, but no stock locked by idle carts.

How it works

  1. Add to cart checks live availability but does not reserve stock.
  2. Opening the checkout summary creates a session and a hold per line. Each hold locks the variant row, checks on_hand - held >= qty, then increments held.
  3. Re-opening the summary with the same selection reuses the session and only adjusts holds whose quantity changed.
  4. Leaving checkout releases the holds. A scheduled job expires stale holds and summary sessions.
  5. On payment the hold is consumed and on-hand stock decreases. On cancel or expiry it is released and only held decreases.

Design decisions

  • Hold at checkout summary, not at add-to-cart or place-order. Holding at add-to-cart lets idle carts block stock; holding at place-order lets a buyer reach payment and then find the item gone.
  • Separate held counter. On-hand stock only changes on a real sale or return, which keeps stock reports honest.
  • Same lock order everywhere (variant, then hold) to avoid deadlocks.
  • Idempotent transitions: releasing a hold that is no longer active does nothing, so overlapping expiry runs cannot double-release.
Loading diagrams…