# Review of design.md (checkout flow, revision 3)

You can start tomorrow, but first add about six short decisions to the doc. The overall design holds up. Stock reservation is atomic, the payment call happens outside any transaction, charges are idempotent, the email can never fail an order, and orders whose charge result is unknown get reconciled. What's missing is mostly what happens when two things touch the same order at once, or when a process dies between steps. Each fix is a sentence or two in the doc plus a test.

## Must fix before coding

**1. Two reconciliation jobs can restore the same stock twice.** You run two instances. If each one runs the 5-minute reconciliation job, both can pick up the same `PENDING` order and both run step 5.
- Case: order X reserved 1 unit of `SKU-A`, which had 10 available, so 9 remain. The provider reports the charge as failed. Instance 1 marks X `FAILED` and restores stock to 10. Instance 2 does the same and restores it to 11.
- Expected: 10.
- Fix: make every status change conditional, e.g. `UPDATE orders SET status='FAILED' WHERE id=:id AND status='PENDING'`. Only restore stock if that update changed a row. Also run the job on one instance only, or claim rows with `SELECT … FOR UPDATE SKIP LOCKED`. The same guard covers a request handler and the job settling the same order at once.

**2. The doc doesn't say what happens when the provider has no record of the charge.** If the service crashes after step 2 commits but before step 3 sends the charge, the order is `PENDING` and the provider has never seen it. Step 6 only says orders get settled as `PAID` or `FAILED`.
- Case: the provider replies "not found". The design doesn't say what the job does next, so the order sits until the 30-minute alert fires and the stock stays locked.
- Fix (recommended): have the job re-send the charge with the same order-id idempotency key instead of only asking for status. That always gives a final answer and can never charge twice. If you'd rather only query, write down that "not found" counts as `FAILED`.

**3. A client-chosen key could return another customer's order.** Step 1 makes `idempotency_key` globally unique. If customer B happens to reuse customer A's key, B gets A's order back. That's a data leak.
- Fix: make the unique constraint `(customer_id, idempotency_key)`. If the same key arrives with a different cart, return `422` instead of the old order.

**4. The doc doesn't say where prices come from.** `order_items.unit_price` and `orders.total` exist, but no table holds catalog prices, and the cart comes from the client.
- Case: the client sends `unit_price: 0.01` and the order is charged 0.01.
- Fix: state the server-side source of prices and that the service computes `total` itself.

## Should fix (cheap now, painful later)

5. **The module rule and step 2 contradict each other.** Step 2 updates `stock` (inventory's table) and inserts into `orders` in one transaction, but the rule says no module touches another module's tables. Pick the mechanism and write it down. My suggestion: the inventory module exposes `reserve(tx, items)`, which takes the caller's transaction, and `OrderCheckout` owns that transaction.
6. **Two orders can deadlock.** Order 1 updates A then B while order 2 updates B then A. Fix: sort items by SKU before updating.
7. **The confirmation email can be lost.** If the process dies after the `PAID` commit and before the email is queued, the email never goes out. Fix: write the email job into the same transaction that sets `PAID` (an "outbox" table that the email worker reads from).
8. **The table definitions are missing keys and a guard.** Add a primary key on `stock.sku`, a primary key on `order_items (order_id, sku)`, and `CHECK (available >= 0)`.
9. **A POST can take about 27 seconds.** That's 4 attempts at 5 s each plus backoff. Check that the load balancer's timeout is longer than that, or the client will see a gateway error instead of the `202`.

## Tests to add to the list

- Reconciliation when the provider says paid, failed, and not found.
- Two reconciliation runs on the same order: stock is restored exactly once.
- Two duplicate `POST /orders` requests at the same moment: one order, one stock reservation.
- The same key reused by a different customer, and the same key sent with a different cart.
- A decline restores stock. Decline is already listed, but say explicitly that the test asserts the stock count.
- A crash between steps 2 and 3, recovered by reconciliation.

## Order lifecycle with the guards in place

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders reserves stock
    PENDING --> PAID: charge ok (only if still PENDING)
    PENDING --> FAILED: decline or "not found" (only if still PENDING), restore stock once
    PENDING --> PENDING: result unknown, 202, reconciliation retries
```

VERDICT: READY WITH CHANGES
