# Review of design.md

The context and code-structure sections are fine. At 2,000 orders a day, load isn't a concern and a single Postgres instance will cope. The problem is the payment part of the flow (steps 3–6). As written, it will charge some customers twice and charge others with no order to show for it. The fixes are well known, but they change how the flow works, so settle them before you write `OrderCheckout`.

## Blocking issues

**1. Retrying the charge can bill a customer twice (step 4).**
A timeout doesn't mean the charge failed. The provider may have processed it, and its reply just didn't reach you in time. Retrying up to 3 times without an idempotency key can charge the customer as many as 4 times.
- **Fix:** send an idempotency key with every charge call (for example, the order id) and reuse the same key on each retry. Check that your payment provider supports this.

**2. The database transaction stays open during the payment call (steps 3–5).**
- **It ties up the database.** Four attempts with 5-second timeouts can keep a transaction and its connection open for 20+ seconds. When traffic spikes, that uses up your connection pool.
- **It leaves no record of the attempt.** If the charge goes through but the commit fails, or the process crashes, the rollback deletes the `PENDING` row. The customer has been charged, and you have no record that an order existed.
- **It hides timeouts as failures.** After 3 timed-out retries, step 6 rolls back and tells the client it failed, even though the charge may have gone through.
- **Fix:** split the flow into separate steps:
  1. Commit the order as `PENDING` and close the transaction.
  2. Call the payment provider with no transaction open.
  3. In a new transaction, set the order to `PAID` or `FAILED`.
  4. If the outcome is unknown, mark it `PAYMENT_UNKNOWN`.
  5. Run a reconciliation job that checks orders stuck in `PENDING` or `PAYMENT_UNKNOWN` with the provider and settles them.

**3. Repeated client requests create duplicate orders (step 1).**
Clients retry when the network drops, and users double-click. Nothing currently stops one cart from becoming two orders.
- **Fix:** require an idempotency key on `POST /orders` (sent in a header) and add a unique constraint on it in the database.

**4. The stock check can oversell (step 2).**
Stock is only read from a cache that can be 5 minutes old, and nothing is reserved or decremented when an order is placed. Two customers can buy the last unit. That's an acceptable trade-off, but it needs to be a written decision:
- either allow occasional overselling and say what happens next (refund, backorder, or apologise to the customer),
- or reserve stock in your own database when the order is placed.

## Should fix (not blocking)

- **Confirmation email (step 5):** send it from an outbox table or a queue after commit, not inline. If the email call fails or hangs, it shouldn't affect the order.
- **Backups:** with nightly backups only, losing the database loses up to a day of paid orders. Turn on point-in-time recovery (WAL archiving); most managed Postgres services offer it.
- **Tests:** one happy-path integration test isn't enough for payments. Add tests for a charge that times out but succeeded, a declined charge, a crash between the charge and the commit, and a repeated `POST`.
- **Status lifecycle:** write down every order status and the allowed transitions between them (`PENDING`, `PAID`, `FAILED`, `PAYMENT_UNKNOWN`, `CANCELLED`).

## Bottom line

None of this means starting over. It's about half a day of rework on the Flow section plus the stock decision. You can begin the module scaffolding tomorrow, but don't build checkout until steps 3–6 are rewritten.

VERDICT: NOT READY
