The design is not safe to build as written, because steps 3–6 can charge a customer twice or charge them and keep no record of the order. The fix is a rewrite of those four steps, not a rethink: the module structure, the single Postgres instance and the overall scope fit a team of four at 200–2,000 orders a day.

## Must change before writing the payment code

**1. Retrying on timeout can charge the customer twice (step 4).**
A timeout means "we don't know", not "it failed". For example, the provider takes 6 s to process a charge and succeeds. We gave up at 5 s and retry, and the provider charges again. The customer pays twice for one order.
- Send an idempotency key (the order id) with every charge call, so retries of the same order collapse into one charge.

**2. A rollback after a successful charge erases the order (steps 3, 5, 6).**
The `PENDING` row lives only inside an open transaction while the payment call is made. For example, the charge succeeds, then the process crashes or the commit fails. The transaction rolls back, so the customer is charged and the database has no trace of the order. The same happens if the last retry times out but the charge actually went through and step 6 rolls back.
- Commit the `PENDING` order first, call the provider with no transaction open, then update the status in a second short transaction.
- Add a status for "outcome unknown" (e.g. `PAYMENT_UNKNOWN`) rather than deleting the order.
- Add a small reconciliation job that asks the provider what happened to orders stuck in `PENDING` or unknown.

This also stops a database connection being held for up to 20 s (4 attempts × 5 s) per checkout.

**3. Nothing stops a duplicate `POST /orders` (step 1).**
For example, the user double-clicks or the mobile client retries on a slow response. Two orders are created and two charges made, because each has its own order id.
- Accept a client-supplied idempotency key and return the existing order on a repeat.

**4. Stock is checked but never reserved (step 2).**
For example, one unit is left and two customers check out in the same 5-minute window. Both pass the cache check and both are charged, for one item. The 5-minute cache is a reasonable tradeoff at this volume, but the design needs to say one of two things:
- **Accept overselling:** write down what happens next (refund or backorder, and who is notified).
- **Prevent it:** decrement a local stock count in the same transaction that inserts the order.

I recommend the second; it is one extra statement and stays within the inventory module's own table.

## Revised flow

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> C[Return existing order]
    B -- no --> D[Transaction 1: reserve stock, insert order PENDING, commit]
    D --> E[Call charge API with order id as idempotency key]
    E -- success --> F[Transaction 2: mark PAID, commit]
    E -- declined --> G[Transaction 2: mark FAILED, release stock]
    E -- timeout after retries --> H[Mark PAYMENT_UNKNOWN, reconciliation job resolves it]
    F --> I[Send confirmation email]
```

## Should change, but not blocking

- **Tests:** one happy-path integration test leaves every case above uncovered. Add integration tests for decline, timeout then success, timeout on all attempts, duplicate request, and last unit of stock. Write them first against a fake payment provider.
- **Email:** the design does not say what happens if sending fails after commit. Sending from a queue or a "to send" table with retry is enough; a failed email must not fail the order.
- **Retry timing:** four attempts at 5 s is a 20 s request, and most clients give up before that. Use one or two retries with a shorter budget and let the reconciliation job handle the rest.
- **Backups:** nightly backups mean losing up to a day of paid orders if the instance dies. Turning on continuous WAL archiving (point-in-time recovery) is cheap and closes that gap.

With steps 3–6 rewritten as in the diagram and the stock decision written down, you can start tomorrow. The orders, inventory and route scaffolding do not depend on any of this, so only the payment path needs to wait for the revision.

VERDICT: READY WITH CHANGES
