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

**The design is sound, but fix four gaps in the document before you start coding.** The core decisions are right. Stock is reserved and the order inserted in one transaction. The payment call happens outside any transaction and sends a stable idempotency key. Unknown payment results go to a reconciliation job instead of being guessed. Nothing here needs a redesign. But each of the four gaps below would ship a real bug, and each takes only a few lines to fix in the doc.

## Order status lifecycle, and where the guards are missing

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (request path or reconciliation)
    PENDING --> FAILED: charge declined (request path or reconciliation), stock restored
    PENDING --> PENDING: result unknown, 202 to client
```

Two things can move an order out of `PENDING`: the request itself and the reconciliation job. The doc doesn't say what happens when both try at once, or when the job runs twice at once.

## Fix before building

**1. Status changes must be conditional, or stock gets restored twice.** Both instances will run the 5-minute reconciliation job. The doc doesn't mention a lock or a single runner.
- Example: an order for 1 unit of SKU X drops stock from 5 to 4, and the charge times out. Both instances' jobs pick the order up in the same minute, and the provider says "declined". Each job runs "mark FAILED and restore stock", so stock ends at 6. It should be 5.
- Fix: every transition is `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock (or queue the email) only if that update changed a row.
- The same guard protects against a slow request path racing the job.
- State in the doc that the job uses the same "settle" code as steps 4 and 5. Then a `FAILED` from reconciliation also restores stock, and a `PAID` from reconciliation also sends the email. Today the doc doesn't say either happens.

**2. Define how reconciliation finds the charge.** After a timeout you have no provider charge ID, and the data model has no column for one.
- Confirm the provider can look up a charge by your idempotency key or metadata. Many can't, and some expire idempotency keys after about 24 hours.
- Define what the job does when the provider answers "no such charge". Re-sending the charge with the same order ID is the safe option. Marking the order `FAILED` is not, if the original request could still land.
- Define what it does when the provider says "still processing": leave the order `PENDING`.
- Add a `provider_charge_id` column for orders where you do get a response.

**3. Lock stock rows in a fixed order, or carts can deadlock.**
- Example: order A has cart [X, Y] and order B has cart [Y, X], and they arrive together. A locks X, B locks Y, and each waits for the other. Postgres kills one, and that customer gets a 500 instead of a 409 or a successful order.
- Fix: sort the SKUs before running the updates in step 2.

**4. Say where prices come from.** Step 1 says "the cart" but doesn't say whether `unit_price` and `total` come from the client. If they do, a client can set its own price. State that the server looks up prices and computes the total.

## Smaller items (fine to fix while building)

- **Duplicate key with a different cart.** Return `422` rather than silently returning the old order.
- **Two concurrent requests with the same key.** The second one fails the unique constraint and its transaction rolls back, which correctly undoes its stock reservation. The code must catch that error and return the existing order, not a 500.
- **Email can be lost.** If the process dies between "commit PAID" and "queue email", no email goes out. If the queue lives in Postgres, write the email job in the same transaction (an outbox). If not, let reconciliation catch `PAID` orders that have no email.
- **No way to check a 202.** Add `GET /orders/{id}` so a client that got `202` can poll. Today the only way is to re-POST with the same key.

## Test plan gaps

Under your TDD standard, the riskiest code here, the reconciliation job, has no tests. Add:
- Reconciliation settles an order as `PAID`, settles it as `FAILED` (stock restored), and handles "charge not found".
- Two settles running on the same order: stock is restored exactly once.
- A crash after a successful charge but before `PAID`: reconciliation recovers the order.
- Two concurrent `POST /orders` with the same key.
- Two concurrent orders for the last unit: exactly one wins and the other gets `409`.
- Carts with SKUs in opposite orders: no deadlock.

The concurrency tests need the real Postgres, not a mock.

The expected scale (2,000 orders a day, two instances, one database) is well within what this design handles. I have no concerns there.

VERDICT: READY WITH CHANGES
