# Review of design.md (order-service checkout, revision 3)

You can start tomorrow, but first make about five small edits to the doc. The overall design is sound: stock is reserved and the order saved in one transaction, the payment call happens outside any transaction, the order id is reused as the provider's idempotency key, and a reconciliation job settles orders whose payment result is unknown. None of the problems below needs a redesign. Two of them would cause real bugs in production: stock reserved forever, and stock restored twice.

## Must fix before building

**1. Reconciliation doesn't say what to do when the provider has no record of the charge.**
- Case: an instance crashes after step 2 commits and before step 3 calls the provider. The order is `PENDING` with stock reserved. Reconciliation asks the provider and gets "charge not found".
- Wrong outcome: the doc only allows `PAID` or `FAILED` (line 38). This third answer isn't handled, so the order stays `PENDING`, the stock stays reserved, and the 30-minute alert fires with no fix.
- Expected: say what happens. Either mark it `FAILED` and restore stock, or retry the charge with the same order id as the key. Also say that a `FAILED` result from reconciliation restores stock like step 5 does, and a `PAID` result queues the email like step 4 does. Right now line 38 says neither.

**2. Status changes aren't guarded, so side effects can run twice.**
- Case: both instances run the reconciliation job, or the job races a slow request. Both see order X as `PENDING`, both get "declined", and both restore stock.
- Wrong outcome: `available` goes up by 2× the quantity, so you sell stock you don't have.
- Expected: write every transition as `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock or queue the email only if that update changed one row. Also decide whether the job runs on one instance only (e.g. a Postgres advisory lock) or whether the guard alone is enough.

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved + order saved (one transaction)
    PENDING --> PAID: charge succeeded<br/>(request or reconciliation)
    PENDING --> FAILED: declined or no charge found<br/>(request or reconciliation)
    PAID --> [*]: queue confirmation email
    FAILED --> [*]: restore stock
```
Only the caller whose guarded `UPDATE ... WHERE status = 'PENDING'` changes a row runs the side effect at the end.

**3. Any customer can read another customer's order through the idempotency key.**
- Case: customer B sends `Idempotency-Key: abc`, a key customer A already used.
- Wrong outcome: the unique constraint is on `idempotency_key` alone, so step 1 hands B customer A's order.
- Expected: make the unique constraint `(customer_id, idempotency_key)` and look up by both. Also decide what happens when the same key arrives with a different cart. Usually that returns `422`.

## Should fix (cheap, or it will bite at 2,000 orders a day)

**4. Deadlocks in the stock reservation.** Order 1 reserves SKU A then B while order 2 reserves B then A. Postgres aborts one of them, and the user sees a 500. Fix: update SKUs in sorted order.

**5. Where does `unit_price` come from?** The data model has no price table. If the price comes from the client's cart, a client can send `unit_price: 0.01`. Say that prices come from a server-side catalog, and name which module owns it.

**6. A `202` client has no way to learn the outcome.** Add `GET /orders/{id}`, or say how the client is told.

**7. Name the email tradeoff.** If the instance crashes between the `PAID` commit and queuing the email (step 4), that email is lost and nothing retries it. That's acceptable at this volume if you write it down. The fix if you need one is an outbox table written in the same transaction.

## Add to the test list
- Reconciliation with each provider answer: paid, declined, not found.
- Two reconciliation runs on the same order: stock is restored once.
- Two identical `POST /orders` arriving at the same time.
- The same key from two different customers.
- Two orders reserving the same SKUs in opposite order.

## Already good, keep as is
- Payment call outside any transaction, with the order id as the provider's idempotency key.
- Email failures never fail the order.
- Thin routes and an `OrderCheckout` service class.
- Modules don't read each other's tables.
- The alert for orders `PENDING` longer than 30 minutes.

Items 1–3 are small edits to the doc. Make them before you write the reconciliation job and the idempotency lookup. You can start on the modules and the reservation transaction right away.

VERDICT: READY WITH CHANGES
