# Review of design.md (checkout flow, order-service)

**Don't build steps 3–6 as written. In two common failure cases, a customer gets charged and has no order.** The rest of the design is sound: the module boundaries, thin routes, a single Postgres instance at this volume, and logging that includes the order id. The fix is a rewrite of the payment flow, which is maybe an hour or two of design work. You can still start tomorrow if you do that first.

## Blockers (must change before building)

**1. Retrying a timed-out charge can charge the customer twice.**
A timeout doesn't mean the charge failed. It means you don't know whether it went through.
- Case: the charge for order 123 reaches the payment provider and succeeds, but the response takes 6 s. Your 5 s timeout fires and you retry. The provider sees a new request and charges again.
- Wrong result: two charges for one order. Expected: one charge.
- Fix: send an idempotency key with every charge, built from the order id; most payment providers support one. The provider then treats your retries as the same request.

**2. The database transaction stays open across the payment call, so a crash or rollback leaves an untracked charge.**
In step 3 the `PENDING` row is inserted inside a transaction that isn't committed until step 5. That row is invisible to everything else, and it disappears on rollback.
- Case: the charge succeeds and then the service crashes, or the commit fails, before step 5 finishes.
- Wrong result: the customer is charged and the database has no record the order ever existed. Expected: an order row that shows the charge was attempted.
- The same thing happens in step 6. After 3 timed-out retries you roll back, but any one of those attempts may have succeeded.
- There's a side cost too. With 4 attempts at 5 s each, one checkout can hold a database connection and its locks for about 20 s.
- Fix: commit `PENDING` first, call the provider with no transaction open, then update the status in a second short transaction. If the outcome is unknown, set the order to `PAYMENT_UNKNOWN` instead of rolling back. A reconciliation job then asks the provider what happened and sets the order to `PAID` or `FAILED`.

**3. A client double-submit creates two orders and two charges.**
A double-click, or a mobile app retrying `POST /orders` after a network drop, produces two orders with different ids. The idempotency key from fix 1 doesn't help, because it's built from the order id. Fix: the client sends its own idempotency key on `POST /orders`. Store it with a unique constraint and return the existing order when the same key comes in again.

The corrected flow:

```mermaid
sequenceDiagram
    participant C as Client
    participant S as OrderCheckout
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (cart, client idempotency key)
    S->>DB: Tx 1: insert PENDING (unique on client key), commit
    S->>P: charge (idempotency key = order id), retry on timeout
    alt success
        S->>DB: Tx 2: mark PAID, queue confirmation email
    else declined
        S->>DB: Tx 2: mark FAILED
    else still unknown after retries
        S->>DB: Tx 2: mark PAYMENT_UNKNOWN
        Note over S,P: reconciliation job asks provider and settles it later
    end
```

## Should change (won't cause wrong charges, but name the decision)

- **Overselling is accepted, but nothing handles it when it happens.** The stock cache can be up to 5 minutes old, and nothing reserves or decrements stock. So two customers can both pass the stock check for the last unit. That tradeoff is reasonable, but the design needs to say what happens next: refund, backorder, or a manual step. It also needs to say which system tells the warehouse that stock was sold. Also, the 800 ms direct call is small next to a payment call that can take several seconds. Check whether you need the cache at all for low-stock items.
- **The confirmation email.** Queue it in the same transaction that marks the order `PAID` (an "outbox" table that a worker sends from), rather than sending it inline after commit. As written, a crash between the commit and the send means the email is never sent. If the send itself fails, nothing in the design retries it.
- **Testing covers only the case that's least likely to break.** One happy-path integration test misses exactly the cases above. Add integration tests for: charge timeout then success (exactly one charge), timeout on every attempt (ends up `PAYMENT_UNKNOWN`), declined card, the same client idempotency key sent twice, and a crash between charge and status update.
- **Backups.** Nightly backups mean you can lose up to 24 h of orders and payments. For financial records, turn on point-in-time recovery, which archives the database's write-ahead log. That's usually a config change on managed Postgres.

## Fine as is
- Going from 200 to 2,000 orders a day is about one order every 40 s at peak. That's no load for a single Postgres instance, and a team of four can run it.
- The module separation, the `OrderCheckout` service class, and structured logging with the order id are all good. Keep them.

VERDICT: NOT READY
