The design is not safe to build as written, but the fixes are small: the checkout flow can charge a customer twice and can take money without recording an order. The scale assumptions, single Postgres instance and module structure are fine.

## Must fix before building

**1. Payment retries can charge twice (step 4).**
A timeout does not mean the charge failed. For example, the provider completes the charge 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) with every charge attempt, so the provider treats retries as the same charge.

**2. A paid order can vanish (steps 3–6).**
The `PENDING` row lives only inside the open transaction. If the charge succeeds and then the service crashes, the commit fails, or all three attempts time out, the rollback erases the order. The customer has been charged, and the database has no trace of it.

Fix: commit the `PENDING` order first, call the provider outside any transaction, then update the order to `PAID` or `FAILED` in a second short transaction. This also stops a database connection being held for up to 15 s during the payment calls.

**3. An unknown payment result is treated as a failure (step 6).**
After three timeouts the design returns an error, but the charge may have gone through. Add a `PAYMENT_UNKNOWN` state and a small job that asks the provider for the charge status and settles those orders.

**4. A repeated `POST /orders` creates a second order.**
A double-click or a mobile client retry produces two orders and two charges. Accept a client-supplied idempotency key and return the existing order on a repeat.

**5. Nothing reserves stock (step 2).**
The check reads a cache up to 5 minutes old, and no step decrements anything. Two customers can both buy the last unit, even seconds apart. The cache is a reasonable choice at this volume, but the design needs one of these:
- **Local reservation:** decrement a local stock count in the same transaction as the order insert.
- **Accepted oversell:** write down what happens when it occurs (refund or backorder, and who is told).

I recommend the local reservation, because it is one extra statement in a transaction you already have.

## Revised flow

```mermaid
sequenceDiagram
    Client->>Service: POST /orders (idempotency key)
    Service->>Postgres: insert PENDING + reserve stock, commit
    Service->>Provider: charge (key = order id), retry on timeout
    alt charge succeeded
        Service->>Postgres: mark PAID, commit
        Service->>Client: 201, then send email
    else charge declined
        Service->>Postgres: mark FAILED, release stock
        Service->>Client: error
    else still unknown after retries
        Service->>Postgres: mark PAYMENT_UNKNOWN
        Service->>Client: 202 "processing"
        Note over Service,Provider: job checks charge status later
    end
```

## Should fix, not blocking

- **Tests:** one happy-path integration test misses the risky cases. Add integration tests for timeout then success, decline, crash between charge and `PAID`, duplicate `POST`, and the last unit of stock.
- **Email:** a failed send after commit is lost silently. Queue it from a table and retry, and never let it fail the order.
- **Backups:** nightly backups mean up to 24 hours of orders lost on a disk failure. Turn on continuous log archiving (point-in-time recovery).

Once items 1–5 are edited into the Flow section, which is about an hour of design work, you can start building tomorrow.

VERDICT: READY WITH CHANGES
