**The design is not safe to build exactly as written, but four targeted edits fix it, and none of them changes the overall shape.** The structure is sound: payment is called outside the transaction, the reconciliation job covers unknown outcomes, email never fails an order, and the module boundaries are clean. The problems are in three mechanisms that can charge a customer twice or sell stock you don't have, plus one data type.

## Must fix before building

**1. Payment retries can charge the customer twice (step 3).**
The key sent to the provider is `<order id>-<attempt number>`, so each retry is a new request to the provider, not a repeat.
- **Case:** attempt 1 times out at 5 s but the provider actually completed the charge. Attempt 2 goes out with key `…-2` and the provider charges again.
- **Expected:** one charge.
- **Fix:** send the order id alone as the key on every attempt, and put the attempt number in a metadata field or your logs for tracing.

**2. A duplicate `POST /orders` creates a second order if it lands on the other instance (step 1).**
Each instance keeps its own in-memory map of keys, and there are two instances.
- **Case:** the client's request reaches instance A, the response is lost, and the retry reaches instance B. B has never seen the key, so it creates a second order, decrements stock again and charges again. A restart of either instance has the same effect.
- **Expected:** B returns the original order.
- **Fix:** store the key in `orders` with a unique constraint on `(customer_id, idempotency_key)`. On a conflict, return the existing row.

**3. Two concurrent buyers can both get the last unit (step 2).**
Reading `available` and then writing `available - quantity` is not protected by the transaction at Postgres's default isolation level.
- **Case:** `available = 1`. Two requests both read 1, both pass the check, both write 0. Two orders are sold against one unit.
- **Expected:** one succeeds, the other gets `409`.
- **Fix:** do it in one statement, `UPDATE stock SET available = available - $q WHERE sku = $sku AND available >= $q`, and treat zero rows updated as short. Update SKUs in sorted order to avoid deadlocks between multi-item carts, and add `CHECK (available >= 0)`.

**4. Money is stored as `double precision`.**
- **Case:** `0.1 + 0.2` gives `0.30000000000000004`, so order totals can drift from the sum of line items and from what the provider charged.
- **Fix:** use integer minor units (cents) or `numeric(12,2)` for `total` and `unit_price`.

## Smaller gaps to close in the doc

- **Process dies between steps 2 and 3:** the order is `PENDING`, stock is held, and no charge was ever attempted. Step 6 only says the job settles orders as `PAID` or `FAILED`; state that "provider has no record of this charge" means `FAILED` and the stock is restored.
- **Stock can be restored twice:** the reconciliation job runs on both instances and can also overlap with step 5. Make every status change conditional (`UPDATE … WHERE status = 'PENDING'`) and restore stock only if that update changed a row.
- **Tests:** the list is good but all sequential. Add two concurrent buyers for the last unit, a duplicate `POST` sent to a different instance, and a crash after commit but before the charge.

Make the four fixes in the doc tonight and you can start building tomorrow.

VERDICT: READY WITH CHANGES
