# Review of design.md: not ready to build on

The code structure and scale assumptions are fine. The payment flow (steps 3–6) has bugs that lose or duplicate money, and fixing them changes the shape of the flow. Fix them in the design before you write code.

## Blockers

1. **Retrying a charge on timeout can double-charge customers.**
   - A timeout doesn't mean the charge failed. The provider may have processed it and the response was lost.
   - Step 4 retries up to 3 times and never mentions an idempotency key.
   - Fix: generate a stable key per order (the order id works) and send it on every attempt. Confirm the provider supports idempotency keys.

2. **A customer can be charged and end up with no order.**
   - The charge happens inside the open DB transaction. If the charge succeeds but the commit fails, the process crashes, or the pod restarts, the transaction rolls back and the `PENDING` row disappears. The money has still been taken.
   - Step 6 has the same problem. After 3 timeouts the design rolls back and returns an error, but one of those attempts may have succeeded.
   - Fix: commit the order as `PENDING` first, then call the provider outside any transaction. Record the outcome in a second write. Add a states for "payment unknown", and a reconciler job that resolves stuck `PENDING` and unknown orders by querying the provider. Use provider webhooks as a second signal.

3. **Backups are nightly on a single instance.** A disk loss can lose up to 24 hours of paid orders. At 2,000 orders a day that is a lot of lost revenue records, and the provider will have taken the money. Fix: WAL archiving or point-in-time recovery, or a managed Postgres, plus a restore test.

## Serious problems

4. **No protection against duplicate client requests.** A user double-clicking, or a client retrying after a network drop, creates two orders and two charges. `POST /orders` needs a client-supplied idempotency key with a unique constraint.

5. **Overselling is unaddressed.**
   - The stock check reads a cache that can be 5 minutes stale, and nothing reserves or decrements stock. Two buyers can both get the last item.
   - Accepting a stale cache is a defensible choice, but the design must say what happens when the warehouse can't fulfill a paid order. Decide between refund, backorder or substitution, and who triggers it.

6. **The request can run for 15 seconds or more.** Three attempts at a 5 s timeout, plus a transaction held open and a DB connection pinned throughout. This ties up connections, and clients will time out first and retry (see item 4). Cap the total budget, or make payment asynchronous with the client polling or receiving a webhook.

7. **A failed confirmation email is lost.** The email is sent after commit with nothing recording it. If the send fails or the process dies, there's no retry. Use an outbox table or queue, which is cheap at this volume.

## Gaps to settle before coding

- **Decline vs. timeout vs. 5xx:** the flow treats all failures the same. A card decline must not be retried and must not look like a timeout.
- **Refund and cancel flows:** none are specified.
- **Server-side pricing:** the cart comes from the client, and the design doesn't say the server recomputes totals.
- **Money handling:** the design doesn't specify currency or whether amounts are stored as integer minor units.
- **Auth, rate limiting and the error contract** for `POST /orders` are not covered.
- **Tests:** one happy-path integration test covers none of the risks above. You need tests for timeout-then-success, crash between charge and commit, duplicate requests and decline.
- **Observability:** logging the order id is good. Also add metrics and alerts on stuck `PENDING` orders and on payment/reconciliation mismatches.

## What's fine

- Module boundaries and thin routes are reasonable for a four-person team.
- A single Postgres instance is adequate for 2,000 orders a day, as long as backups are fixed.
- Accepting the cache over the 800 ms warehouse call is a reasonable tradeoff once the oversell policy is defined.

## Next step

Revise the flow around three changes: commit `PENDING` first, charge with an idempotency key outside the transaction, and add a reconciler. That is probably a few hours of design work rather than a rewrite. After that, start building.

VERDICT: NOT READY
