# Review of design.md (revision 2)

**Not ready as written.** Three parts of the flow would charge customers twice, sell stock you don't have, or create duplicate orders once both instances are taking traffic. Each fix is small and leaves the overall shape alone (modules, the `OrderCheckout` class, reconciliation, the alert). If you make these changes in a revision 3 tonight, you can still start tomorrow.

## Blockers (must fix before building)

**1. Payment retries can charge the customer twice (step 3).**
The provider's idempotency key is `<order id>-<attempt number>`, so every retry looks like a new charge to the provider.
- *Case:* attempt 1 for order `A` succeeds at the provider, but the response arrives after your 5 s timeout. Attempt 2 is sent with key `A-2`. The provider charges again, and the customer pays twice. They should have been charged once.
- *Fix:* use one key per order, the order id, on every attempt. To trace attempts in the provider's dashboard, put the attempt number in the charge's metadata or description, not in the key. Reconciliation also needs this: with one key per order it knows which charge to ask about.

**2. Two buyers can both get the last unit (step 2).**
Reading `available` and then writing `available - quantity` doesn't protect you under Postgres's default isolation level (READ COMMITTED).
- *Case:* `available = 1`. Two requests each read 1, both write 0, and both commit. Two orders are taken for one unit. One should have got `409`.
- *Fix:* decrement in a single statement, `UPDATE stock SET available = available - $q WHERE sku = $sku AND available >= $q`, and treat "0 rows updated" as "short, return 409". Update items in sorted `sku` order so two carts sharing SKUs can't deadlock each other. Add `CHECK (available >= 0)` as a backstop.

**3. Duplicate requests create duplicate orders (step 1).**
The idempotency keys live in a map inside each instance, and there are two instances behind the load balancer. A restart or deploy also wipes the map.
- *Case:* a client's `POST /orders` with key `K` times out on its side, so it retries. The retry lands on the other instance, which has never seen `K`, and you get a second order and a second charge. The client should have got the existing order back.
- *Fix:* store the key in Postgres, e.g. `idempotency_key text UNIQUE` on `orders` or a separate table. Insert it in the same transaction as the order, and when the unique constraint is violated, return the existing order. Also decide what happens when the same key arrives with a different cart. The usual answer is `422`.

**4. Money is stored as `double precision` (`orders.total`, `order_items.unit_price`).**
- *Case:* three items at 0.10 add up to `0.30000000000000004`. That fails equality checks against the provider's amount and drifts in reports. It should be exactly 0.30.
- *Fix:* use `numeric(12,2)`, or integer minor units (cents) to match the provider's API.

## Should fix (gaps that will bite soon after launch)

5. **Reconciliation doesn't say how it settles.**
   - When it settles an order as `FAILED`, it must restore stock too, in the same transaction, the way step 5 does.
   - Both instances will run the job. The request path can also finish the same order at the same moment. Every settlement must be a conditional update, `UPDATE orders SET status = … WHERE id = $id AND status = 'PENDING'`, and stock should only be restored when that update hit a row. Otherwise stock gets restored twice.
   - Say what happens when the provider has no record of the charge (the request never reached it). Probably `FAILED` plus restock.
6. **Confirmation emails can be lost (step 4).** If the process crashes after the `PAID` commit and before the email is queued, the email never goes. Write an outbox row in the same transaction as `PAID`, and have a worker send from that table.
7. **The module rule contradicts step 2.** "No module reads another module's tables", yet one transaction writes both `stock` and `orders`. State the mechanism: for example, the inventory module exposes `reserve(tx, items)` and `release(tx, items)`, which take the caller's transaction. Then the boundary holds and the transaction stays atomic.
8. **A client that gets `202` has no way to learn the outcome.** Add `GET /orders/{id}`, or say how the client finds out.
9. **Missing keys and constraints.** `order_items` has no primary key and no foreign key to `orders`. `stock.sku` isn't declared as the primary key. `status` has no `CHECK` constraint.

## Test plan gaps

The integration list covers the happy path and the simple failures, but not the cases above. Add:
- **Two concurrent buyers for the last unit:** exactly one gets `201`, the other gets `409`, and `available` ends at 0.
- **Duplicate `POST /orders` hitting different instances**, or two app processes against one database.
- **Timeout where the first charge actually succeeded:** the fake provider must be able to "succeed but respond late". Assert there was one charge.
- **Reconciliation:** settles as paid, settles as failed with stock restored, provider has no record, and reconciliation racing the request path. Assert stock is restored only once.
- **Money totals:** an exact-arithmetic check on a cart like 3 × 0.10.

## What's already good

Charging outside the database transaction, a reconciliation job for unknown outcomes, an email failure never failing the order, the 30-minute `PENDING` alert, thin routes, and order-id-tagged logs are all the right calls. Keep them.

VERDICT: NOT READY
