**Not as written, but you can start tomorrow if you make four changes to the doc first.** The design has three ways to lose money or stock under ordinary conditions (a timeout, a retry, two simultaneous buyers) and one data-type error. The overall shape is sound and none of the fixes changes it.

## Must fix before building

1. **A payment timeout can charge the customer twice (step 3).** The provider key is `<order id>-<attempt number>`, so each retry looks like a new charge to the provider.
   - **Case:** attempt 1 is charged by the provider but the response takes 6 s, so the service times out and sends attempt 2 with a new key. The customer is charged twice; the expected result is one charge.
   - **Fix:** use one key per order (the order id) on every attempt, and trace attempts through metadata or logs.

2. **A retried `POST /orders` can create a second order (step 1).** Each instance keeps idempotency keys in its own memory, and there are two instances.
   - **Case:** the client times out and retries with the same key, and the load balancer sends the retry to the other instance. The result is two orders, two stock deductions and two charges; the expected result is the original order returned. A restart or deploy also wipes the map.
   - **Fix:** store the key on `orders` with a unique constraint on `(customer_id, idempotency_key)`, inserted in the step 2 transaction. On a conflict, return the existing order without starting another charge.

3. **Two buyers can both get the last unit (step 2).** The service reads `available`, then writes `available - quantity`. Under Postgres's default isolation, two transactions can both read the same value.
   - **Case:** stock is 1 and two orders for 1 unit arrive together. Both read 1, both write 0, and both orders are accepted; the expected result is one order and one `409`.
   - **Fix:** `UPDATE stock SET available = available - $qty WHERE sku = $sku AND available >= $qty`, and treat zero rows updated as short. Process SKUs in sorted order to avoid deadlocks, and add `CHECK (available >= 0)`.

4. **Stock can be restored twice for one failed order (steps 5 and 6).** Nothing stops the request handler and the reconciliation job, or the job running on both instances, from settling the same order.
   - **Case:** a decline arrives late while the job is settling the same order. Both mark it `FAILED` and both add the stock back, so `available` ends up 1 too high.
   - **Fix:** make every transition `UPDATE orders SET status = ... WHERE id = ... AND status = 'PENDING'`, and only restore stock or queue the email if that updated a row. Run the job under an advisory lock so only one instance runs it.

5. **Money is stored as `double precision`.** Floats cannot represent most decimal amounts exactly, so totals drift by a cent (0.1 + 0.2 is not 0.3). Use integer minor units or `numeric(12,2)` for `total` and `unit_price`.

## Decide and write down (short, but currently unspecified)

- **Reconciliation when the provider has no record of the charge.** This happens if the service crashes between step 2 and step 3. I recommend marking the order `FAILED` and restoring stock, which is safe once fix 1 is in and the order is older than the retry window.
- **Confirmation email after a crash or reconciliation.** Step 4 queues the email after the commit, so a crash in between loses it, and the reconciliation path never mentions email. Write an outbox row in the same transaction as the `PAID` update and send from that.
- **Where prices come from.** There is no price table, and the doc does not say whether `unit_price` comes from the client's cart. It must be looked up server-side, or a client can set its own price.
- **How the client learns the outcome after a `202`.** There is no `GET /orders/{id}` in the doc.
- **Cross-module transactions.** Steps 2 and 5 span the orders and inventory tables in one transaction, but "no module reads another module's tables" does not say how. State that `OrderCheckout` owns the transaction and passes it into each module's functions.
- **Missing schema details.** The doc has no primary keys, no `CHECK` on `status`, and no provider charge id on `orders`.

## Tests to add to the plan

The current list would pass with all the bugs above present. Add:

- A timeout where the fake provider did charge, followed by a retry, asserting exactly one charge.
- A duplicate `POST` sent concurrently and across two instances.
- Two concurrent buyers of the last unit.
- The handler and the reconciliation job settling the same order, asserting stock is restored once.
- Reconciliation for each provider answer: paid, declined, not found.

## What holds up

The payment call sits outside the database transaction, email never fails an order, unknown outcomes stay `PENDING` with a reconciliation job and an alert, and one Postgres database is ample for 2,000 orders a day.

VERDICT: READY WITH CHANGES
