# Review of design.md: not ready to build

The scale and code structure are fine. The checkout flow (steps 3–6) has money-handling bugs that would reach production as written. These are in order of severity.

## Blockers

1. **Retries can double-charge customers (step 4).**
   - A timeout doesn't mean the charge failed. The provider may have charged the card and answered slowly.
   - Retrying up to 3 times with no idempotency key can charge the customer up to 4 times.
   - Fix: send a stable idempotency key per order, such as the order id plus an attempt-independent token, on every charge call.

2. **A timeout followed by a failure leaves a charged customer with no order (steps 4 and 6).**
   - If all attempts time out, step 6 rolls back and tells the client it failed. The charge may still have gone through.
   - The same happens if the process crashes, or the commit fails, after the charge succeeds. The money is taken and nothing records it.
   - Fix: treat the outcome as `UNKNOWN`, not failed. Persist the order and the payment attempt before calling the provider, in a committed transaction. Then reconcile unknown outcomes by querying the provider or handling its webhooks.

3. **The payment call runs inside an open DB transaction (steps 3–5).**
   - With 4 attempts at 5 s each, a connection is held for 20 s or more per order, and any provider slowdown ties up your pool.
   - Rolling back also destroys the only record that an attempt happened. That is the audit trail you need for point 2.
   - Fix: commit `PENDING` first, call the provider outside any transaction, then do a short second transaction to mark `PAID` or `FAILED`.

4. **No idempotency on `POST /orders` itself.**
   - If a client retries after a network drop or a double-click, it creates a second order and a second charge.
   - Fix: require a client-supplied idempotency key with a unique constraint.

5. **Backups are nightly on a single instance.**
   - A disk loss or bad migration can lose up to 24 hours of orders and payments.
   - For a service that takes money, you need WAL archiving or point-in-time recovery. Also test a restore before launch.

## Serious gaps

6. **Stock check can oversell.**
   - The cache is up to 5 minutes stale, and nothing reserves or decrements stock.
   - The design says "we accept the cache" but not what happens when you oversell. Do you cancel and refund, or backorder? Decide that now.
   - The 800 ms argument is weak. Checkout isn't a 100 ms path, and you could do an authoritative check for low-stock items only.

7. **The confirmation email is sent inline after commit (step 5).**
   - If sending fails, the email is lost. If the process dies between commit and send, the email is lost silently.
   - Use a transactional outbox or a `confirmation_sent` flag with a retry job.

8. **The state machine is too small.**
   - `PENDING` and `PAID` don't cover `FAILED`, `PAYMENT_UNKNOWN`, `CANCELLED` or `REFUNDED`.
   - Define the allowed transitions explicitly.

9. **The test plan covers the wrong paths.**
   - Only the happy path has an integration test, but the risk is in timeouts, retries, crashes mid-flow and duplicate requests.
   - Add a fake payment provider that can time out, succeed late and return duplicates, and test those cases.

## Missing from the doc

- Server-side price and total calculation. The design never says the cart totals aren't trusted from the client.
- Auth on `POST /orders`.
- How card data is handled. Use the provider's tokenization so you never touch PANs.
- Refunds and cancellation.
- Metrics and alerts. Structured logs alone won't tell you about stuck `PENDING` orders; alert on their age.

## What's fine

- At 2,000 orders a day, one Postgres instance and a single service are appropriate. Don't add queues or microservices for scale.
- The module boundaries, thin routes and `OrderCheckout` class are reasonable.

## Suggested path to build tomorrow

You can start on the parts that don't depend on the flow: schema, modules and logging. Rewrite steps 3–6 first, which is probably half a day of design:

1. Persist the order and a payment-attempt row, with idempotency keys, and commit.
2. Call the provider outside any transaction.
3. Record the outcome. Reconcile unknowns with a job or webhook.
4. Send the email through an outbox.

VERDICT: NOT READY
