# Review of design.md: not ready to build

The scale assumptions and code structure are fine. The payment flow (steps 3–6) can lose or duplicate money, and I'd fix it before writing any code.

## Blockers

**1. Retrying the charge with no idempotency key can double-charge.**
- Step 4 retries on a 5 s timeout, up to 3 times. A timeout doesn't mean the charge failed. The provider may have processed it and been slow to answer.
- Without a stable idempotency key per order, each retry can create a new charge.
- Fix: generate the key when the order is created, store it on the order, and send it on every attempt.

**2. Rolling back after a possibly successful charge leaves the customer charged with no order.**
- The `PENDING` insert sits inside an uncommitted transaction (step 3) that stays open during the charge.
- If all retries time out, step 6 rolls back and tells the client it failed. The charge may still have gone through.
- If the process crashes or the commit fails after the charge succeeds, the order row never existed. No record exists to reconcile against.
- Fix:
  - Commit `PENDING` first, with the idempotency key.
  - Call the provider outside any transaction.
  - Update to `PAID` or `FAILED` in a second short transaction.
  - Treat "unknown" (timeout) differently from "declined". Unknown stays `PENDING` and a reconciler job resolves it by querying the provider with the same key.
  - Also use the provider's webhook if it has one.
- As written, holding a DB connection through up to ~15 s of retries is also a bad idea, though it won't matter at your volume.

**3. `POST /orders` has no idempotency, so client retries create duplicate orders.**
- A mobile client that times out and retries will create a second order, and with fix 1 alone, a second charge.
- Fix: require a client-supplied idempotency key with a unique constraint.

**4. Nothing decrements or reserves stock, so you can oversell.**
- The stock check reads a cache up to 5 minutes stale, and nothing updates it after an order.
- Two customers can both buy the last unit, and a popular item can be oversold for the whole 5-minute window.
- Accepting a stale cache is defensible at 200 orders a day.
- The doc still has to say what happens when you oversell: cancel and refund, or backorder? Without a defined policy, the cancel/refund path will be invented under pressure.
- Cheap mitigation: keep a local reserved-quantity count and subtract it from the cached stock.

**5. Nightly backups on a single Postgres instance put up to 24 hours of paid orders at risk.**
- Losing a day of orders that customers have already paid for is not acceptable.
- Fix: enable WAL archiving or point-in-time recovery. This is a config change on most managed Postgres offerings.
- Also write down a recovery target and test a restore at least once.

## Should fix before shipping

- **Email:** "send after commit" doesn't say what happens if the send fails. It must not fail or roll back the order. At minimum, record `email_sent_at` and retry from a job. An outbox table is the clean version.
- **No order state machine:** only `PENDING` and `PAID` are named. Define `FAILED`, `CANCELED`, `REFUNDED` and the legal transitions. Refunds are needed for the oversell policy anyway.
- **Test plan covers the wrong path:** one happy-path integration test doesn't touch the riskiest code. Add integration tests for these cases, using a fake provider:
  - Timeout, then success on retry.
  - Timeout, but the charge actually went through.
  - Decline.
  - Crash between charge and commit.
  - Duplicate client request.
- **Module boundaries:** "no module reads another's tables" is good. But `OrderCheckout` orchestrates all three, so say how stock reservation and payment calls are exposed (interfaces, not tables).
- **Missing basics:** no mention of auth, how the cart's prices are verified server-side, or currency/money types (use integer minor units). Retry backoff is also unspecified.
- **Observability:** the order id in logs is good. Add an alert on orders stuck in `PENDING` longer than N minutes. That is how you'll find problems 1 and 2 in production.

## What's fine

- Volume is tiny, so there's no need for queues, sagas, or microservices. A single Postgres plus a reconciler job is enough. Don't over-build.
- Thin routes and a service class are fine.
- Accepting an 800 ms warehouse call versus a stale cache is a reasonable trade-off once the oversell policy is written down.

## Next step

This is probably an hour or two of rewriting steps 3–6 and adding a state machine, not a redesign. You could start on scaffolding (modules, logging, schema) tomorrow. I wouldn't implement the checkout flow until the revised version is written and re-reviewed.

VERDICT: NOT READY
