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

**No, not as written.** The payment step can charge a customer twice, or take their money and keep no order. Both cases come from how step 4 (retry the charge) and steps 3–6 (one transaction around the charge) are designed. You'd write this logic first, so fix the design before you build it. The fix is well known and small, probably a couple of hours of rework, not a redesign. Everything else is fine or a small addition.

## Must fix before building

**1. Retrying the charge can double-charge (step 4).**
Example: the provider charges the card but its response takes 6 s. We hit the 5 s timeout and retry, and the provider charges again. The customer pays 2× (up to 4× with 3 retries). They should pay once.
A timeout doesn't tell you the charge failed, only that you don't know. **Fix:** send an idempotency key with every charge (the order id works), so the provider treats all retries as one charge. Check that your provider supports this. Most do.

**2. The charge runs inside an open database transaction (steps 3–6).**
- **Money taken, no order:** the charge succeeds, then the commit fails or the process restarts. The rollback removes the order, but the customer has still paid. We should have a `PAID` order.
- **Charge we don't know about:** all 4 attempts time out, so we roll back and return an error. But one of those attempts may have gone through. The customer is told it failed and is still charged.
- **`PENDING` is never visible:** it's only ever stored inside an uncommitted transaction, so after a crash nothing tells you a payment was in progress.
- A database connection stays held for up to about 20 s (4 × 5 s) while we wait on a third party.

**Fix:** commit `PENDING` first, charge outside the transaction, then update the order in a second short transaction. Add a scheduled job that finds orders stuck in `PENDING` and asks the provider what happened.

```mermaid
flowchart TD
    A[POST /orders] --> B[Check stock in inventory cache]
    B --> C[Tx 1: insert order PENDING, commit]
    C --> D[Charge with idempotency key = order id<br/>retry on timeout]
    D -->|success| E[Tx 2: mark PAID]
    D -->|declined| F[Tx 2: mark FAILED]
    D -->|still unknown| G[Leave PENDING]
    E --> H[Send confirmation email]
    G --> I[Job: query provider, then mark PAID or FAILED]
```

**3. Say what happens when stock is oversold.** Using the 5-minute cache is a reasonable tradeoff at this volume. But two customers can buy the last unit within the same 5 minutes, and both will be charged. The design needs to say what happens next: refund and apology email, backorder, or reserving stock with the warehouse at payment time. Pick one and write it down, because it decides whether you need a `CANCELLED`/refund path.

## Should add (can happen alongside building)

- **Make `POST /orders` safe to retry.** Clients that time out will retry, especially since a request can now take 20 s. A client-supplied idempotency key stops one cart from becoming two orders.
- **Say what happens if the confirmation email fails.** It shouldn't fail or undo a paid order. Send it after the commit, ideally from a queue or retry job.
- **Test the failure paths, not just the happy path.** At minimum: a charge that times out and then succeeds on retry (charged once), a declined card, a crash between charge and commit (the job fixes it), a stock-out, and an email failure. Today the plan has one integration test, for the happy path only.
- **Backups:** nightly backups mean losing up to a day of paid orders. Turn on point-in-time recovery (continuous WAL archiving, or the managed-database equivalent).

## Already fine
- 2,000 orders a day (~1.4 per minute) is light load. A single Postgres instance is fine.
- The module boundaries (no module reads another's tables), thin routes, an `OrderCheckout` service class, and structured logs with the order id are all sound. Log the idempotency key and the provider's charge id as well.

**Next step:** rewrite steps 3–6 as in the diagram, add the idempotency key and the stuck-`PENDING` job, and pick the oversell policy. After that it would be ready, and you could start tomorrow.

VERDICT: NOT READY
