The design is sound and sized correctly for 2,000 orders a day, but it has two gaps that would corrupt stock or strand orders in production. Both are small additions to the document, so you can start building tomorrow once they are written in.

## Fix before building

1. **The reconciliation job has no rule for "provider has never heard of this order".**
   - **Failing case:** an instance is restarted (a deploy is enough) after step 2 commits but before the charge request in step 3 reaches the provider.
   - **What happens:** the order is `PENDING`, the stock is reserved, and the provider has no charge. Step 6 only allows settling as `PAID` or `FAILED`, so the order stays `PENDING` forever, the stock stays held, and the 30-minute alert fires on every such order.
   - **Expected:** the order is marked `FAILED` and the stock restored.
   - **Fix:** add the rule that "no charge found" settles as `FAILED` with stock restored. Make sure the 2-minute age threshold stays longer than the worst-case retry window in step 3, which is about 30 s depending on the backoff.

2. **Nothing stops an order from being settled twice.**
   - **Failing case:** both instances run the reconciliation job at the same 5-minute tick and both see the same `PENDING` order with a declined charge.
   - **What happens:** both mark it `FAILED` and both restore stock, so `available` ends up higher than the real inventory and you oversell later.
   - **Expected:** stock is restored once.
   - **Fix:** make every status change conditional (`UPDATE orders SET status = ... WHERE id = ... AND status = 'PENDING'`) and only restore stock or queue the email when that update changed a row. This also covers a slow request in steps 4 or 5 racing the job.

## Decide and write down (cheap now, annoying later)

- **Lock order for stock:** two concurrent orders reserving the same SKUs in opposite order (A then B, B then A) will deadlock in step 2. Reserve in sorted SKU order.
- **Idempotency key scope:** the unique constraint should be on `(customer_id, idempotency_key)`. With a global key, a collision returns one customer's order to another.
- **Reused key, different cart:** state what happens. Returning `422` is the usual choice.
- **Concurrent duplicate:** the second request hits the unique violation on insert, and must catch it and return the existing order instead of a `500`.
- **Price source:** the design does not say where `unit_price` and `total` come from. They must be computed server-side, never taken from the client's cart.
- **Outcome after `202`:** the client needs a way to learn the result, such as `GET /orders/{id}`. It is not in the design.
- **Schema constraints:** add primary keys, a foreign key and primary key on `order_items`, and `CHECK (available >= 0)` on `stock`. Add a column for the provider's charge id so support and reconciliation can trace a payment.
- **Lost email:** a crash between the `PAID` commit and queuing the email in step 4 loses the email. Either accept that explicitly or have the reconciliation job sweep `PAID` orders with no email queued.
- **Module boundary:** "no module reads another module's tables" conflicts on its face with step 2, which changes `stock` and `orders` in one transaction. Say that the orders module calls an inventory function that joins the caller's transaction.

## Tests to add to the plan

- A crash after reserving stock and before the charge, with the job then settling the order as `FAILED` and restoring stock.
- The reconciliation job itself: provider says paid, declined, and not found.
- Two settlements of the same order, with stock restored exactly once.
- Two concurrent buyers for the last unit, where exactly one gets it.
- Two concurrent requests with the same idempotency key.
- Multi-SKU orders placed concurrently in opposite SKU order.

VERDICT: READY WITH CHANGES
