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

The context, the scale assumptions and the code-structure section are fine. A single Postgres instance and one service class are right for 200 to 2,000 orders a day. The problem is the payment part of the flow, steps 3 to 6. As written, it will charge some customers twice and charge others without leaving an order on record. That's the main thing the design has to get right, and it needs reworking before you build it.

## Blocking issues

**1. Retrying the charge on timeout can charge the customer twice (step 4).**
A 5 s timeout doesn't mean the charge failed. Often it went through and only the response was lost. Retrying up to 3 times without an idempotency key can charge the customer up to 4 times.
- **Fix:** Send one idempotency key with every attempt, such as the order id. Most payment providers support this. If the result is still unknown after the retries, mark the order `PAYMENT_UNKNOWN` and resolve it by asking the provider. Don't treat it as a failure.

**2. The payment call runs inside an open database transaction (steps 3 to 6).**
- **Charged with no order:** If the charge succeeds but the commit fails, or a timed-out charge actually went through, the rollback erases the `PENDING` row. The customer has paid and you have no record of it. The design has no reconciliation step to catch this.
- **Held connections:** Each checkout can hold a database transaction and connection open for about 20 s (4 × 5 s). This depends on the speed of an outside service, and a slow provider can use up your connection pool.
- **Fix:** Insert `PENDING` and commit first. Call the payment provider outside any transaction. Then, in a second transaction, mark the order `PAID` or `FAILED`, or `PAYMENT_UNKNOWN` if the result is unclear. Add a small job that sweeps up `PENDING` and `PAYMENT_UNKNOWN` orders older than N minutes and checks them with the provider.

**3. Repeat requests from the client create duplicate orders (step 1).**
Users double-click and mobile clients retry on flaky networks. Each `POST /orders` makes a new order and a new charge.
- **Fix:** Have the client send an `Idempotency-Key` header, store it with a unique constraint, and return the existing order when a key repeats.

## Should fix before or during the build

**4. Stock is never reserved, so items can oversell (step 2).**
Checking against a cache that's up to 5 minutes old is a reasonable choice. But nothing decrements or reserves stock, so two orders can both buy the last item.
- **Fix:** Decide what happens when you oversell (cancel and refund, or backorder), write it down, and build that path. Alternatively, keep a local reservation count on top of the cache.

**5. The confirmation email is sent inline (step 5).**
If the email provider is slow or fails, it shouldn't affect the checkout or the client's response.
- **Fix:** Queue the email after commit, using an outbox table or a job.

**6. Only the happy path has an integration test.**
The risky paths are the ones that need tests: payment timeout followed by a successful retry, an unknown payment result, a repeated client request, and a commit failure after a successful charge.

**7. Nightly backups can lose up to a day of orders.**
For payment records, that's a lot of data to lose. Turning on WAL archiving or point-in-time recovery (most managed Postgres offerings support it) is cheap and fixes this.

## What's good
- The module boundaries (no module reads another's tables), thin routes and a single `OrderCheckout` class are well suited to a team of four.
- Logging the order id on every line will help a lot when you reconcile payments.
- Choosing the cache over an 800 ms warehouse call is a sensible tradeoff. It just needs the oversell policy from item 4.

## Bottom line
Fixing the design is probably half a day of rewriting steps 3 to 6 into a short state machine (`PENDING` → `PAID`, `FAILED` or `PAYMENT_UNKNOWN`) with idempotency keys and a reconciliation job. You can start tomorrow on the module scaffolding, the inventory cache and the routes. Don't build `OrderCheckout` until the payment flow is redesigned.

VERDICT: NOT READY
