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

You can start building tomorrow, but fix two gaps in the reconciliation job first. Both lose or invent stock. The core design is sound: the stock reservation is atomic, the payment call happens outside any transaction, the order id is reused as the payment idempotency key, and orders with an unknown payment result get settled later. Everything else below is a short clarification you can make while you build.

## Order lifecycle and where the gaps are

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved, order inserted
    PENDING --> PAID: charge succeeds (request or reconciliation)
    PENDING --> FAILED: decline (request), stock restored
    PENDING --> FAILED: reconciliation, stock restore NOT specified
    PENDING --> PENDING: provider says "no such charge", NOT specified
```

## Problems, by severity

**High: fix in the design before coding these parts**

1. **When reconciliation marks an order `FAILED`, it doesn't put the stock back.** Step 5 restores stock only on the decline path inside the request. Example: a customer buys the last unit, the charge times out, and reconciliation later finds it was declined. The order becomes `FAILED` but `available` stays at 0, so the unit is lost for good. The fix: reconciliation should use the same "mark FAILED and restore stock in one transaction" routine as step 5.
2. **Two reconciliation jobs can settle the same order, so stock gets restored twice.** You run two instances, and the design doesn't say the job runs on only one of them. Example: both instances pick up a `PENDING` order that was declined, and each adds 1 back to stock. `available` goes up by 2 when it should go up by 1. The fix: every status change should be a conditional update (`UPDATE orders SET status = ... WHERE id = :id AND status = 'PENDING'`). Restore stock only if that update changed a row. This also protects against a late request and the job racing each other.

**Medium: decide early in the build**

3. **There's no rule for when the provider has no record of the charge.** If every attempt timed out before reaching the provider, the reconciliation job finds no charge. As written, the order stays `PENDING` forever, and the 30-minute alert pages someone for a case you could handle automatically. Suggestion: once an order is older than N minutes and the provider has no charge, mark it `FAILED` and restore stock.
4. **The design doesn't say where prices come from.** The client sends the cart, and `order_items.unit_price` and `orders.total` get stored, but there's no price table and no step that looks prices up. If the server trusts prices sent by the client, a customer can pay whatever they like. State that the server looks prices up itself and name the module that owns them.
5. **A client that gets `202` has no way to learn the outcome.** No `GET /orders/{id}` is defined, and the design doesn't say whether the client should poll it.
6. **Some tests are missing.** Your test list doesn't cover:
   - the reconciliation job settling `PAID`, settling `FAILED` (with stock restored), and the provider-has-no-record case
   - two reconciliation runs on the same order
   - two concurrent `POST /orders` calls with the same key
   - two buyers racing for the last unit
   - a crash after the charge succeeds but before the order is marked `PAID`
   - a decline restoring stock (currently implied, not listed)

**Low: clarify while you build**

7. **Simultaneous duplicate requests aren't handled.** Two requests with the same key both reserve stock, and then one fails on the unique constraint. That's safe because it rolls back, but the code has to catch that specific error and return the existing order instead of a 500. Also decide what happens when a key is reused with a different cart. The usual answer is `422`.
8. **Reserving several SKUs can deadlock.** Example: order A reserves SKU X then SKU Y while order B reserves Y then X. The database stops one of them with a deadlock error. At 2,000 orders a day this is rare, but sorting the SKUs before the updates removes it completely.
9. **The confirmation email can be lost.** If the service crashes between committing `PAID` and queuing the email, no email goes out. The reconciliation `PAID` path doesn't queue the email at all. An outbox row written in the same transaction as the status change fixes both.
10. **The module rules conflict with step 2.** "No module reads another module's tables" doesn't fit one transaction that touches both `stock` and `orders`. Say how this works, for example: the inventory module exposes a `reserve(tx, items)` function that runs inside a transaction the caller passes in.
11. **The design is missing details:**
    - Retries should apply only to timeouts and 5xx errors, never to declines.
    - The worst case is about 20 seconds of charge attempts plus backoff. Check that this fits inside the client and load-balancer timeouts.
    - `stock.sku` and `order_items` have no primary keys.

None of these needs a redesign. Items 1 and 2 are each a few lines in the design doc, and the rest can be settled as you reach that part of the code.

VERDICT: READY WITH CHANGES
