# Review: order-service checkout design (`design.md`)

**Short answer:** The code structure, team size and expected load are all fine. The problem is the checkout flow itself (steps 3–6). As written, it will charge some customers twice and charge others without creating their order. That logic is the core of the service and the hardest part to fix after launch, so revise it before you build.

## Blocking issues

**1. Retries can charge a customer twice (`design.md:14-15`).**
A timeout doesn't mean the charge failed. The provider may have charged the card and the response got lost. Each retry without an idempotency key can then charge the customer again.
- **Fix:** Send an idempotency key with every charge, such as the order id. Use the same key on every retry.
- **Fix:** When retries run out on a timeout, the result is "unknown," not "failed." Mark the order `PAYMENT_UNKNOWN` and settle it later using the provider's webhook or a reconciliation job.

**2. The charge happens inside an open database transaction (`design.md:13-18`).**
- If the charge succeeds but the commit fails, or the process crashes, the rollback deletes the order. The customer has paid and you have no record of it.
- On a timeout, step 6 rolls back an order that may actually have been paid.
- Each checkout can hold a database connection and transaction open for about 20 s (4 attempts × 5 s).
- **Fix:**
  1. Commit the `PENDING` order first.
  2. Call the payment provider outside any transaction.
  3. Update the status to `PAID`, `FAILED` or `PAYMENT_UNKNOWN` in a second short transaction.
  
  Failed orders stay in the table, which also gives you an audit trail.

**3. A double submit creates duplicate orders.**
`POST /orders` has no idempotency key. A client whose request times out after 20 s will probably retry and create a second order, and the payment-side fix in issue 1 won't stop it.
- **Fix:** Accept an `Idempotency-Key` header from the client and enforce it with a unique constraint.

**4. Stock is checked but never reserved (`design.md:10-12`).**
The cache can be up to 5 minutes old, and nothing decrements or reserves stock. Two buyers can both get the last item, and anything sold in the last 5 minutes still looks available.
- Accepting the cache is reasonable. But the design should either:
  - reserve stock atomically (in a local stock table, or through a reservation call to the warehouse), or
  - state that overselling is accepted and describe what happens when it does (cancel and refund, backorder, etc.).

## Should fix

**5. Backups.** Nightly backups mean you could lose up to a day of paid orders. Turn on WAL archiving or point-in-time recovery; most managed Postgres services include it.

**6. Email.** Sending the email inside the request means an email outage either fails or slows down checkout. Put it on a queue or outbox table and send it asynchronously.

**7. Tests.** One happy-path integration test isn't enough for this flow. Add tests for:
- a payment timeout followed by a successful retry, with exactly one charge
- a decline
- a double submit
- a crash between the charge and the status update

**8. Client contract.** Say what the client gets back when the payment result is unknown. A 202 with the order id and a status to poll is one option.

## Fine as is

- The module boundaries, the thin routes, the `OrderCheckout` class and structured logging with the order id are all good choices.
- 2,000 orders a day is very low load. One Postgres instance will handle it easily.

Fixing items 1–4 only means rewriting the Flow section, probably an hour or two of work. Once that's done, you can start building.

VERDICT: NOT READY
