# Review of design.md

This design isn't ready to build from yet. The context and code structure are fine, but the checkout flow (steps 3–6) can charge customers twice, or charge them and then lose the order. That flow is the core of the service, so fix it before you write `OrderCheckout`. The fixes are standard and should take an afternoon of design work, not a rewrite.

## Blocking: money correctness

**1. Retrying on timeout can charge the customer twice (step 4).**
A timeout doesn't mean the charge failed. The provider may have taken the money and the response got lost. Retrying up to 3 times without an idempotency key could charge the customer up to 4 times.
- **Fix:** send an idempotency key with every charge call, derived from the order id, so retries are safe. Check that your payment provider supports this; most do.
- **Fix:** if you're still unsure after the last retry, mark the order `PAYMENT_UNKNOWN` and settle it later by checking with the provider. Don't roll back.

**2. The database transaction stays open during the payment call (steps 3–6).**
- A customer can be charged with no order on record. If the charge succeeds and then the commit fails (crash, deploy, lost connection), the rollback deletes the only record of an order that was paid for.
- A plain failure has the same problem. Step 6 rolls back even when the failure came after an ambiguous timeout, so you lose the record you'd need to reconcile.
- Each checkout holds a connection and transaction for up to about 20 s (4 × 5 s). That's poor practice on a single Postgres instance, even at 2,000 orders a day.
- **Fix:** split the flow into separate steps:
  1. Commit the order as `PENDING` in its own transaction.
  2. Call the payment provider outside any transaction.
  3. Update the order to `PAID`, `FAILED` or `PAYMENT_UNKNOWN` in a second transaction.
- **Fix:** add a small background job that finds orders stuck in `PENDING` or `PAYMENT_UNKNOWN` and checks them with the provider.

**3. The client can create duplicate orders by retrying (step 1).**
If the client's `POST /orders` times out (likely, with up to 20 s of payment retries) and the client retries, you get a second order and a second charge. Have the client send an `Idempotency-Key` header and store it on the order with a unique constraint.

## Should fix before launch

**4. Stock can be oversold (step 2).**
The cache is up to 5 minutes old, and nothing reserves stock, so two customers can both buy the last item. Using the cache is a reasonable trade-off, but the design needs to say what happens next:
- Either reserve or decrement stock in the warehouse system after payment.
- Or accept overselling and define how you handle it (refund, backorder, and how the customer is told).

**5. The confirmation email can be lost (step 5).**
If the process dies after the commit but before the email goes out, the email never sends. Write an "email pending" row in the same transaction that marks the order `PAID`, and have a worker send from that table (an outbox pattern). It also keeps a slow email provider out of the checkout request.

**6. Nightly backups can lose up to 24 hours of paid orders.**
For payment data, turn on point-in-time recovery. Most managed Postgres services offer it as a setting.

**7. The tests only cover the happy path.**
The risky paths are the failure cases. Add integration tests for:
- a payment timeout followed by a successful retry
- a timeout on every attempt (`PAYMENT_UNKNOWN`)
- a crash between the charge and the commit
- a duplicate client request

A fake payment provider that can be told to time out makes these straightforward.

## What's good
- The volume figures are realistic, and a single Postgres instance handles 2,000 orders a day easily.
- Splitting orders, payments and inventory into modules that don't read each other's tables is the right shape.
- The thin routes plus an `OrderCheckout` class, and the order id on every log line, are good choices.
- Writing down why you accept the cache's staleness is the kind of trade-off record a design should have.

## Recommendation
Rewrite the Flow section with states `PENDING → PAID / FAILED / PAYMENT_UNKNOWN`, idempotency keys on both the payment call and `POST /orders`, no transaction open during the payment call, and a reconciliation job. Once that's written down, the rest can be built as designed. You could start tomorrow on the module skeletons, routes, logging and the inventory cache, since none of those depend on the fix.

VERDICT: NOT READY
