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

The design is mostly sound, and you can start building tomorrow if you first fix three gaps in how orders get settled. Each fix is a sentence or two in the doc plus a test, not a redesign. The idempotency-key handling, keeping the payment call outside the transaction, the reconciliation job and the 30-minute alert are all good choices for 200 to 2,000 orders a day.

## Fix before building

**1. Two settlements of the same order can restore its stock twice.**
Both instances run the reconciliation job, and a slow request thread can also finish late. Steps 4–6 say "mark the order X" but don't say the update only applies while the order is still `PENDING`.
- Failing case: order for 2 × SKU-A, stock at 5. Instance 1 and instance 2 both see the order as `PENDING`, both get "declined" from the provider, and both run the step 5 transaction. Stock goes to 9. It should be 7.
- Fix: every change of status is `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock only if that update changed a row, in the same transaction.

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (only if still PENDING)
    PENDING --> FAILED: declined, or provider has no charge (only if still PENDING, and stock restored in same transaction)
    PAID --> [*]
    FAILED --> [*]
```

**2. Reconciliation doesn't say what happens when the provider has no record of the charge.**
If an instance crashes after step 2 commits but before step 3 sends anything, the order is `PENDING` and stock is reserved, but there is no charge. The job asks the provider and gets "not found". The design only covers settling as `PAID` or `FAILED`, so this order stays `PENDING` forever and its stock never comes back.
- Fix: say what "not found" means. Either settle as `FAILED` and restore stock, or send the charge again with the same order-id key. Also state that settling as `FAILED` from reconciliation restores stock. Step 6 doesn't say this today, but step 5 does.

**3. Reserving several items can deadlock.**
- Failing case: order A reserves SKU-X then SKU-Y, and order B reserves SKU-Y then SKU-X, at the same moment. Postgres detects the deadlock and aborts one of them. That customer gets a 500, although a 409 or a success would have been correct.
- Fix: sort the SKUs before running the `UPDATE`s. Add a `CHECK (available >= 0)` on `stock` as a safety net.

## Worth deciding, not blocking

- **Duplicate POSTs that arrive at the same time.** Neither request finds an existing order, so one of them fails on the unique constraint. Catch that violation, roll back, and return the existing order. Also decide what happens when the same key arrives with a different cart. A 422 is the usual answer.
- **How a client learns the outcome after a `202`.** There's no `GET /orders/{id}` in the design. Add one, or say the client re-POSTs with the same key.
- **Request duration.** The payment call allows 4 attempts × 5 s plus backoff, so one request can take more than 20 s. Check that this fits within the load balancer's and the client's timeouts. Also say that declines are not retried; only timeouts and 5xx errors are.
- **The confirmation email can be lost.** If an instance crashes after `PAID` commits but before the email is queued, the email never goes out, and nothing catches it. To fix that, write the email job in the same transaction (an outbox table). Otherwise, accept the loss and write it down as known debt.
- **Module boundary.** Step 2 updates `stock` and `orders` in one transaction, but inventory "owns" `stock`. State that the inventory module exposes a reserve/release function that takes the caller's transaction. Otherwise this becomes a hidden cross-module write.
- **Schema.** Give `order_items` and `stock` primary keys (`(order_id, sku)` and `sku`).

## Tests to add to the plan

- Reconciliation settles as `PAID`, settles as `FAILED` (with stock restored), and handles "provider has no charge".
- Two reconciliation runs on the same order restore stock only once.
- Two orders compete for the last unit at the same time: one succeeds, one gets a 409.
- Two orders with the same SKUs in opposite order: neither gets a 500.
- Two `POST /orders` with the same key at the same time: one order is created and both get it back.
- Same key, different cart: rejected.
- Crash after the order is reserved and before it is charged: reconciliation cleans it up.

VERDICT: READY WITH CHANGES
