← Technical notes
Confidential / NDA3 diagrams

Cart and stock reservation

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

TL;DR

NDA note: a shared cart that checks availability on add, and holds stock only when the buyer opens the checkout summary.

live trace · Opening checkout summaryfrom the original diagram
Buyer Summary API API Checkout session Variant Stock hold 01open summary of cart lines 02lock existing or create session 03lock row, check on hand minus held 04create hold, active 05held = held + quantity 06totals and expiry
  1. 01
    Buyer → Summary APIopen summary of cart lines
  2. 02
    API → Checkout sessionlock existing or create session
  3. 03
    API → Variantlock row, check on hand minus held
  4. 04
    API → Stock holdcreate hold, active
  5. 05
    API → Variantheld = held + quantity
  6. 06
    API → Buyertotals and expiry
01

Background

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

02

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 checkout the hold is consumed and on-hand stock decreases. On cancel or expiry it is released and only held decreases.
03

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 checkout 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.
All diagrams

Other views

Tap a table to see what it links to.

BUYER
CART_LINE
VARIANTint on_handint held
STOCK_HOLDint quantitystring statetimestamp expires_at
CHECKOUT_SESSION
INVENTORY_EVENT
  • BUYER has CART_LINE 1 → many
  • VARIANT referenced by CART_LINE 1 → many
  • VARIANT reserved by STOCK_HOLD 1 → many
  • CHECKOUT_SESSION owns STOCK_HOLD 1 → many
  • VARIANT logged INVENTORY_EVENT 1 → many

← Technical notes on the home page