# Review of design.md: not ready yet, but the fixes are small

The context, code structure and scale assumptions are fine. A team of four, a modular service and one Postgres instance easily handle 2,000 orders a day. The problem is steps 3–6 of the payment flow. As written, they can charge customers twice, or charge them and leave no order behind. Since this is the core of the design, fix it before you write `OrderCheckout`.

## Blockers

**1. Retrying a timed-out charge can charge the customer twice (step 4).**
A timeout doesn't mean the charge failed. Often it went through and only the response was lost. Retrying up to 3 times without an idempotency key could charge the same customer as many as 4 times.
- **Fix:** Create an idempotency key once per order (the order id works), save it with the order, and send the same key on every retry. Most providers support this (for example, Stripe's `Idempotency-Key`). After the last retry times out, mark the order `PAYMENT_UNKNOWN` instead of failed, and check its real status with the provider later.

**2. The database transaction stays open during the payment call, and a rollback deletes the evidence (steps 3–6).**
- With 4 attempts at 5 s each, one transaction can stay open for 20+ seconds. That ties up a database connection and any locks it holds, so a slow provider can block the whole service.
- Rolling back on failure deletes the `PENDING` row. If the charge actually went through (a timeout, or a crash before commit), the customer has paid and you have no record of the order.
- **Fix:** Use separate short transactions:
  1. Insert the order as `PENDING` (with its idempotency key) and commit.
  2. Call the provider outside any transaction.
  3. Update the order to `PAID`, `FAILED` or `PAYMENT_UNKNOWN` and commit.

  Add a small job that checks orders stuck in `PENDING` or `PAYMENT_UNKNOWN` against the provider. If the provider offers webhooks, use them as well.

**3. Nothing stops the client from creating the same order twice (step 1).**
If a request takes 20 s, the client or a proxy will time out and send it again. Nothing in the design stops that second request from creating a second order and a second charge.
- **Fix:** Require an `Idempotency-Key` header on `POST /orders` and store it with a unique constraint. If the same key comes in again, return the original result.

## Should fix (can be decided this week)

**4. Stock is never reserved (step 2).**
Checking a cache that can be 5 minutes old is a reasonable trade-off. But no step ever decrements or reserves stock, so two buyers can buy the last item, and so can every buyer between cache refreshes. Write down what happens when you oversell: refund, backorder, or a reservation call to the warehouse after payment. Decide who handles those cases.

**5. A failed email can't be retried (step 5).**
If sending the email fails or the process crashes after commit, nobody retries. Write an "email pending" record (an outbox row) in the same transaction that marks the order `PAID`, and have a worker send it. This also keeps email failures out of the checkout request.

**6. Nightly backups can lose up to a day of paid orders.**
Turn on WAL archiving / point-in-time recovery (most managed Postgres services include it). It's cheap, and it's very hard to rebuild a day of payments from the provider's records alone.

**7. Only the happy path has an integration test.**
The risky cases are the failures. Add integration tests for a declined card, a provider timeout followed by a successful retry, a timeout on every attempt, and a duplicate request with the same idempotency key. Use a fake payment provider for these.

## Minor

- Set a limit on how long the client waits in total. With the retries above, the provider call can block for 20 s. Consider returning `202 Accepted` with the order status if payment isn't settled within a few seconds.
- Log the payment provider's charge id next to the order id. You'll need both when reconciling.

## Starting tomorrow

You can start on the module scaffolding, the thin routes, logging and the inventory cache tomorrow, since none of that depends on the changes above. Before you write the checkout and payment code, update the Flow section for blockers 1–3. That's about one rewritten section, and probably half a day of work.

VERDICT: NOT READY
