**The design is not safe to build as written: steps 3–6 can charge a customer twice, or charge them and keep no record of the order.** Both are fixed by reordering the payment steps, which is about an hour of design revision, and the code structure section can stand as is.

## Must fix before writing payment code

**1. Retrying on timeout can double-charge (step 4).**
A timeout does not mean the charge failed.
- Case: the provider charges the card but responds in 6 s. The service times out at 5 s, retries, and the provider charges again.
- Expected: one charge. Actual: two, or up to four.
- Fix: send an idempotency key (the order id) with every charge call, so the provider treats retries as the same charge.

**2. Rolling back on failure can leave a charge with no order (steps 3–6).**
The `PENDING` row lives inside an uncommitted transaction, so a rollback or a crash erases it.
- Case: every attempt times out, but the first one actually succeeded at the provider. The service rolls back and returns an error.
- Expected: an order row you can reconcile or refund. Actual: the customer is charged and the database has no trace of the order.
- The same happens if the process dies between the charge and the commit.
- Fix: commit `PENDING` first, charge outside any transaction, then update the status in a second short transaction. Add a small job that finds orders stuck in `PENDING` and asks the provider what happened.

**3. `POST /orders` has no protection against client retries (step 1).**
- Case: the request takes 12 s during provider retries, the mobile client gives up and resubmits, and two orders are created and charged.
- Fix: the client sends an idempotency key, and the service returns the existing order for a repeated key.

**4. Nothing reserves or decrements stock (step 2).**
The design checks stock but never says where it is reduced.
- Case: two customers order the last unit seconds apart. Both pass the check and both are charged.
- The 5-minute cache widens this window, but a fresh read would have the same gap.
- At 200–2,000 orders a day the cache itself is a reasonable trade. What is missing is a decision: either reserve stock atomically in your own table, or accept overselling and write down what happens then (refund, backorder, who is notified).

## 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 (idempotency key)
    S->>DB: Reserve stock + insert order PENDING, commit
    S->>P: Charge (idempotency key = order id), retry on timeout
    alt charged
        S->>DB: Mark PAID, commit
        S->>C: 201
        S-->>C: Confirmation email (async)
    else declined
        S->>DB: Mark FAILED, release stock, commit
        S->>C: Error
    else still unknown after retries
        S->>C: 202 "processing"
        Note over S,P: Reconciliation job resolves PENDING orders
    end
```

## Should fix, but does not block starting

- **Tests cover only the happy path.** Every bug above is on a failure path. Add integration tests for: timeout then success, decline, crash after charge, duplicate `POST`, and two orders for the last unit.
- **An email failure must not fail the order.** Send it after the commit, from a queue or outbox table, and state whether a lost email is acceptable.
- **Nightly backups can lose up to 24 hours of paid orders.** Turning on continuous WAL archiving for point-in-time recovery is cheap on one instance.
- **The order states are not defined.** List them (`PENDING`, `PAID`, `FAILED`, and possibly `REFUNDED`) with the allowed transitions.

## What is good

The module boundaries, thin routes, the `OrderCheckout` service class and logging the order id on every line are right for a team of four at this volume. A single Postgres instance is plenty for 2,000 orders a day.

Tomorrow, revise steps 2–6 and decide the oversell policy first; module scaffolding and the orders and inventory work can start in parallel.

VERDICT: READY WITH CHANGES
