# Review of design.md

**Short answer:** the code structure section is fine. The checkout flow (steps 3–6) has problems that will take customers' money without giving them an order, or charge them twice. These need fixing in the design before anyone writes `OrderCheckout`. It's probably an evening's rework, not a redesign.

## Blocking issues

**1. Retrying the charge after a timeout can charge customers twice (step 4).**
A timeout doesn't mean the charge failed. The provider may have processed it and the response got lost. Retrying up to 3 times without an idempotency key can charge one order up to 4 times.
→ Send an idempotency key with every charge, derived from the order id. Most providers support this. Confirm yours does before you build.

**2. The payment call happens inside an open database transaction (steps 3–6).**
- **Lost payments:** if the charge succeeds but the commit fails (crash, deploy, connection drop), the customer has paid and you have no order. Step 6's rollback removes the only record that the attempt happened.
- **Pointless PENDING row:** the uncommitted `PENDING` row is invisible to everything else, so it can't be used for recovery or reconciliation.
- **Long-held connections:** with up to 4 tries × 5 s, a transaction can stay open for more than 20 s. At 2,000 orders a day with bursts, that can exhaust the connection pool.

→ Commit `PENDING` in its own transaction. Then call the provider with no transaction open. Then update to `PAID` or `FAILED` in a second transaction.

**3. No plan for unclear outcomes.**
After 3 timeouts the design returns an error to the client, but the customer may already have been charged.
→ Add a state for "payment outcome unknown". Add a reconciliation job that queries the provider for stuck `PENDING` orders and resolves them. Tell the client "we're confirming your payment", not "failed".

**4. Clients that retry `POST /orders` create duplicate orders.**
A double-click or a mobile retry on a flaky network produces two orders and two charges. The provider's idempotency key from point 1 doesn't help, because each request creates a new order id.
→ Accept a client-supplied `Idempotency-Key` header on `POST /orders` and dedupe on it.

## Should fix (not blocking)

**5. Stock is checked but never reserved (step 2).**
A cache up to 5 minutes old with no reservation or decrement means two customers can buy the last item. Using the cache is a reasonable tradeoff against 800 ms calls, but the design needs to say what happens on oversell: refund, backorder, or a reservation call to the warehouse after payment. Pick one.

**6. Email in the request path (step 5).**
If sending the email fails or hangs after commit, what does the client get? Send it asynchronously, using an outbox table or a job queue, so email trouble can't affect checkout.

**7. Nightly backups mean up to 24 hours of lost orders.**
Those orders have real charges behind them. Turn on WAL archiving / point-in-time recovery. It's cheap, and you'll want it before you reach 2,000 orders a day.

**8. Tests cover only the happy path.**
Add integration tests for:
- payment timeout followed by a successful retry (exactly one charge)
- payment declined
- crash between the charge and marking the order `PAID` (the reconciliation job recovers it)
- a duplicate `POST /orders`

## What's good
- Clear module boundaries, with no module reading another's tables.
- Thin routes, with the flow in one service class.
- The order id on every log line. That will make reconciliation much easier.
- Using the cache is a sensible call at this scale.

Fix points 1–4, which mostly means rewriting steps 3–6 into the commit → charge → update sequence with idempotency keys and a reconciliation path. After that, you can start building.

VERDICT: NOT READY
