# Review of design.md

**Short answer: not as written.** Most of the doc is fine. The problem is the payment part of the flow (steps 3–6): built as described, it can charge a customer twice, or charge them and keep no record of the order. That's the part you'd build first, so fix it before you write code. It's probably an hour of design work, not a rethink.

## Must fix before building

**1. Retrying the charge after a timeout can double-charge (step 4).** A timeout doesn't mean the charge failed. Often it went through and the response was lost. Retrying up to 3 times without an idempotency key can charge the customer as many as 4 times.
- **Fix:** send an idempotency key on every charge call, for example the order id; most payment providers support this. Only retry when the result is unknown (timeouts, 5xx errors), never after a decline. Wait a bit longer between each retry.

**2. The database transaction stays open during the payment call (steps 3–6).**
- **Charged with no order:** if the charge succeeds but the commit then fails, or the process crashes, the rollback erases the `PENDING` row. The customer has paid and you have no order.
- **Wrong error after a timeout:** if all retries time out, step 6 rolls back and tells the client the order failed, even though the charge may have gone through.
- **Connections tied up:** worst case is 4 × 5 s = 20 s+ per checkout, holding a database connection the whole time. Slow periods with the provider will use up your connection pool.
- **Fix:** split it into separate transactions:
  1. Insert `PENDING` and commit.
  2. Charge the card, outside any transaction.
  3. In a new transaction, set the order to `PAID` or `FAILED`.
  4. If the result is still unknown, leave the order `PENDING` and tell the client "processing", not "failed".
  5. Add a reconciliation job that finds stale `PENDING` orders and checks the provider for what actually happened.

**3. A client retry of `POST /orders` creates a duplicate order.** Mobile clients and double-clicks will cause this. Take an idempotency key from the client, or a cart/checkout id, and make it unique in the database.

## Should fix

**4. Overselling (step 2).** A cache that can be 5 minutes old, with no reservation or stock decrement, means two customers can buy the last unit. At 2,000 orders a day that may be an acceptable risk, but write the policy down: what happens when an order can't be fulfilled (refund, backorder, or notify the customer), and who handles it. If that's not acceptable, reserve or decrement stock when the order is placed.

**5. The confirmation email is sent inline (step 5).** If sending fails or hangs after the commit, the order should still succeed. Send it in the background (an outbox table or a job queue) and retry on failure.

**6. Testing (code structure).** One happy-path integration test doesn't cover the risky parts. Add tests for:
- a timeout followed by a retry, checking the customer is charged exactly once
- a declined card
- a crash between the charge and the commit
- a duplicate `POST`
- reconciliation of stuck `PENDING` orders

## Worth considering

**7. Backups.** Nightly backups mean up to 24 hours of paid orders could be lost. Point-in-time recovery (WAL archiving) is cheap, and a good fit once real money is involved.

## What's fine

- The expected load is no problem for a single Postgres instance.
- Keeping each module out of the others' tables, thin routes with the flow in `OrderCheckout`, and logging the order id on every line are all good choices. Keep them.

## Plan for tomorrow

You don't have to wait to start. Set up the project and build the module structure, inventory and orders while you rewrite steps 3–6 using points 1–3 above. Don't write the payment code until that rewrite is done.

VERDICT: NOT READY
