The design is not safe to build as written: the payment step in the Flow section can charge a customer twice or charge them and keep no order record. The fixes are small and well understood, and the Code structure section, single Postgres instance and module boundaries are right for 200 to 2,000 orders a day.

## Must fix before writing the payment code

**1. Retrying on timeout can double charge (step 4).**
A timeout means "no answer", not "the charge failed". For example, the provider charges the card in 6 s, the service gives up at 5 s and retries, and the customer is charged twice for one order.
- **Fix:** send an idempotency key (the order id) on every charge attempt so the provider treats retries as the same charge. Confirm your provider supports this; most do.

**2. A failure after a successful charge erases the order (steps 3 to 6).**
The `PENDING` insert and the charge sit inside one open transaction. For example, the charge succeeds, then the process crashes or the commit fails, the insert rolls back, and the customer has paid with no row in the database to refund against. The same happens if all three attempts time out but one actually went through.
- **Fix:** commit the `PENDING` order first, charge outside any transaction, then mark it `PAID` or `FAILED` in a second short transaction.
- **Also add:** a small scheduled job that finds orders stuck in `PENDING` and asks the provider what happened to them.
- **Side benefit:** a database connection is no longer held for up to 15 s while waiting on the provider.

**3. Duplicate `POST /orders` creates duplicate orders (step 1).**
The request can take 15 s or more in the worst case. If the client times out at 10 s and the user taps "Pay" again, there are two orders and two charges.
- **Fix:** have the client send an idempotency key with the request, put a unique constraint on it, and return the existing order on a repeat.

## Revised flow

```mermaid
sequenceDiagram
    participant C as Client
    participant S as order-service
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (cart, idempotency key)
    S->>DB: Check stock, insert order PENDING, commit
    S->>P: Charge (key = order id), retry on timeout
    P-->>S: Success or decline
    S->>DB: Mark PAID or FAILED, commit
    S-->>C: Result
    S->>C: Confirmation email (after commit)
```

## Decide before building, smaller in scope

- **Stock is never reserved or decremented.** The flow checks a cache that is up to 5 minutes old and never reduces the count. Two customers can buy the last unit within the same window. The cache itself is fine at this volume, but the design needs to say what happens on an oversell (refund, backorder) and whether the service decrements its own count when an order is placed.
- **Email failure is undefined.** If the send fails after commit, the order is paid but the customer hears nothing. A retry and a logged failure are enough at this scale.
- **The test plan covers only the happy path.** Every defect above lives in a failure path. Add integration tests for a timeout followed by success, a decline, a crash between charge and `PAID`, and a repeated `POST /orders`. A fake payment provider that can be told to time out makes these cheap.
- **Nightly backups can lose up to a day of paid orders.** Turning on continuous WAL archiving (point-in-time recovery) closes that gap without changing the design.

You can start tomorrow if the Flow section is rewritten to the revised flow first; the three must-fix items change the order of the transaction and the charge, so they are costly to retrofit.

VERDICT: READY WITH CHANGES
