The design is not safe to build as written, but it only needs changes to steps 3–6 of the flow, and you can start tomorrow if you make them first. The code structure and the single Postgres instance are fine for 200–2,000 orders a day.

## Must fix before building

**1. The payment retry can charge the customer more than once.**
Step 4 retries on a 5 s timeout, but a timeout does not mean the charge failed. For example, the provider takes 6 s and succeeds, the service gives up at 5 s and retries, and the customer is charged twice (up to four times with three retries).
- **Fix:** send an idempotency key (the order id) with every charge call, so the provider treats retries as the same charge.
- **If the provider has no idempotency keys:** look up the charge status before retrying.

**2. A customer can be charged and end up with no order record.**
The `PENDING` insert sits in a transaction that is still open during the charge call, so nothing is saved until step 5 commits. There are two failing cases:
- **Crash after charge:** the charge succeeds, then the process crashes or the commit fails. The customer has paid, but the order row is gone.
- **All attempts time out:** step 6 rolls back and returns an error, but one of the charges may have gone through, and there is no row left to reconcile against.

**Fix:** commit `PENDING` in its own short transaction, charge with no transaction open, then update to `PAID` or `FAILED` in a second short transaction. Never delete the row. This also stops a database connection being held for up to 20 s per checkout.

**3. Nothing cleans up orders left in `PENDING`.**
Once fix 2 is in, a crash or a full set of timeouts leaves a `PENDING` row. The design needs a small scheduled job that asks the provider for the charge status and settles the order as `PAID` or `FAILED`.

**4. `POST /orders` itself can create duplicates.**
If the client retries after a dropped connection or a double-click, it creates a second order and a second charge. Accept a client-supplied idempotency key, store it with a unique constraint, and return the existing order on a repeat.

**5. The test plan covers only the happy path.**
The costly bugs here are all on failure paths. Add integration tests, with a fake payment provider, for:
- timeout then success
- card declined
- all attempts time out
- crash between charge and `PAID`
- duplicate `POST /orders`

## Revised flow

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> C[Return existing order]
    B -- no --> D[Check stock in inventory cache]
    D --> E[Insert order as PENDING and commit]
    E --> F[Charge payment provider, order id as idempotency key, retry on timeout]
    F -- success --> G[Mark PAID and commit]
    F -- declined --> H[Mark FAILED and commit, return error]
    F -- all attempts time out --> I[Leave PENDING, return 'payment being confirmed']
    G --> J[Send confirmation email]
    I --> K[Scheduled job checks charge status with provider]
    K --> G
    K --> H
```

## Decide, but not blocking

- **Overselling:** the flow never reserves or decrements stock, and the cache is up to 5 minutes old, so two customers can both buy the last unit. At this volume that is acceptable if you write down what happens next (refund or backorder, and who is notified).
- **Email failure:** state that a failed confirmation email is logged and retried, and does not fail the order.
- **Backups:** nightly backups mean a disk failure can lose up to a day of paid orders. Turning on Postgres point-in-time recovery closes that gap cheaply.
- **Request duration:** four attempts at 5 s each is 20 s, and most clients will give up before then. Consider one retry, with the scheduled job handling the rest.

VERDICT: READY WITH CHANGES
