# Review of design.md: checkout flow for order-service

**It isn't ready to build from yet.** The payment step can charge a customer twice, or charge them and leave no order on record. Both problems are in the core flow (steps 3–6), so the code would be built on them. The fix is small. It's a restructure of those steps, not a new architecture, and you could probably rewrite them tonight.

## Blockers (must fix before building)

**1. Retrying the charge after a timeout can charge the customer twice.**
A timeout doesn't mean the charge failed. It means you don't know what happened.
- Case: attempt 1 reaches the provider and succeeds, but the response arrives after your 5 s timeout. You retry, and attempt 2 also succeeds.
- Actual: the customer is charged twice for one order.
- Expected: one charge.
- Fix: send an idempotency key with every charge call. The order id works. Most providers (Stripe, Adyen, Braintree) support this, so a retry returns the original result instead of charging again. Confirm your provider supports it before you write the code. If it doesn't, check the charge status before each retry instead of retrying blindly.

**2. The database transaction stays open across the payment call, and a rollback can erase a paid order.**
- Case: attempt 1 succeeds at the provider but times out on your side. Attempts 2 and 3 time out too. Step 6 rolls back.
- Actual: the customer is charged and the `PENDING` row is gone. You have no record to reconcile against, and the customer gets an error.
- Expected: the order survives in a state that says "payment outcome unknown," and something resolves it later.
- A second cost: each checkout holds a database connection and its locks for up to about 20 s (4 attempts × 5 s). At 2,000 orders a day the average load is low, but a slow afternoon at the provider can exhaust the connection pool and stall every other request.
- Fix: split the work into short transactions with the external call between them (diagram below).

**3. Nothing stops the client from creating a duplicate order.**
The client can wait up to about 20 s. Mobile clients and load balancers often time out sooner and retry `POST /orders`. That creates a second order and a second charge, and the fix for blocker 1 won't catch it because the new order has a new id.
- Fix: have the client send an `Idempotency-Key` header, and put a unique constraint on that key.

### Proposed flow

```mermaid
flowchart TD
    A[POST /orders with Idempotency-Key] --> B{Key seen before?}
    B -- yes --> R[Return the existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D[Transaction 1: insert order PENDING, commit]
    D --> E[Charge payment provider, idempotency key = order id, retry on timeout]
    E -- success --> F[Transaction 2: mark PAID and write email job to outbox table, commit]
    E -- declined --> G[Transaction 2: mark FAILED, commit, return error]
    E -- still unknown after retries --> H[Leave as PAYMENT_UNKNOWN, return 'processing']
    H --> J[Reconciliation job asks provider for status, sets PAID or FAILED]
    F --> I[Worker sends confirmation email from outbox]
```

The outbox is a table of pending email jobs, written in the same transaction as the `PAID` update. A worker sends them afterwards. This keeps a failed email send from affecting the order, and it means no confirmation is lost if the process crashes right after the commit.

## Should fix (not blockers, but decide them on purpose)

**4. A stale cache can sell stock you don't have.** The cache can be up to 5 minutes old and nothing reserves stock, so two customers can both buy the last unit. Accepting the cache to avoid an 800 ms call is a reasonable choice at this scale. But the design needs to say what happens when you oversell: refund automatically, backorder, or have someone contact the customer. Right now it's undefined. If oversells are expensive for your business, reserve the stock in your own database inside transaction 1. That isn't a call to the warehouse.

**5. Up to a day of paid orders could be lost.** With a single instance and nightly backups, losing the instance means losing every order since the last backup, and all of those customers have already been charged. Turning on WAL archiving or point-in-time recovery (built in if you're on RDS or Cloud SQL) costs little and closes most of that gap.

**6. The test plan only covers the case that's least likely to break.** One happy-path integration test leaves the failure paths untested, and that's where the money is lost. Under your own tests-first standard, add these before you start:
- A timeout followed by a successful retry produces exactly one charge (use a fake payment provider).
- Every attempt times out, and the order ends in `PAYMENT_UNKNOWN`, not deleted.
- The same `Idempotency-Key` sent twice returns the same order.
- A declined card ends in `FAILED` with no outbox row.
- A failed email send leaves the order `PAID`.

## What's fine as is

- The scale assumptions (2,000 orders a day is about one every 40 s on average). This doesn't need queues, sagas, or more database instances, so don't add them.
- The module boundaries, with no module reading another's tables, and the thin routes with an `OrderCheckout` service class.
- Structured logging keyed on order id. Also log the provider's charge id, which you'll need for reconciliation.

## Next steps

Rewrite steps 3–6 to match the diagram, choose an oversell policy, confirm your payment provider supports idempotency keys, and add the failure-path tests to the plan. After that, I'd expect this to be ready.

VERDICT: NOT READY
