Rebasely Beta: data may be purged

examples/storefront Public

Markdown · 22 lines · 1.1 KiB · at c6a00ae

---id: 0004-take-stock-at-checkouttype: decisionowner: justinstatus: acceptedreviewed: 2026-10-02---# 0004 Take stock at checkout, not when items go in the cart## ContextTwo shoppers can want the last item. The stock has to be taken at some point between adding it to a cart and paying for it. Holding it from the moment it goes in the cart is the friendliest for the shopper who got there first, but most carts are abandoned, and each one would hold stock until a timer released it.## DecisionAdding to a cart only checks the stock ([[carts]]). Checkout takes it, in the same D1 batch that creates the order, and the `CHECK (stock >= 0)` constraint on `products` refuses a batch that would oversell ([[checkout]]). Stock comes back if the order is cancelled.## Consequences- No reservations to expire for abandoned carts; a cart costs nothing until checkout.- A shopper can see an item in their cart and then be told at checkout that it sold out. The error lists what is left so the client can offer the remaining quantity.- Stock is held from checkout until the payment succeeds or the order is cancelled, so unpaid orders need to expire.