The design is close but not ready. The core holds up: reserving stock with a conditional update, charging outside the transaction, using the order id as the payment idempotency key, and running a reconciliation job are all the right choices. Volume isn't a concern either, since 2,000 orders a day is small for one Postgres. The problems are all in how an order leaves `PENDING`. Four of them can lose stock or a customer's confirmation, and each needs only a few sentences in the doc.

## Must fix before building

**1. When the reconciliation job marks an order `FAILED`, the stock never comes back.** Step 5 restores stock when a charge is declined. Step 6 only says the job "settles them as `PAID` or `FAILED`."
- *Case:* a customer orders 1 unit of SKU A. The charge times out on every attempt, and later the provider reports it failed. The job marks the order `FAILED`. Stock for A stays one lower forever.
- *Fix:* say that every path to `FAILED` (decline or reconciliation) uses the same "fail and restore stock" transaction.

**2. Two settlers can process the same order, so stock can be restored twice.** Both service instances run the reconciliation job every 5 minutes, and the request path can also settle orders.
- *Case:* instances 1 and 2 both pick up order X (`PENDING`, 3 units of A). Both are told it failed, and both restore stock. A goes up by 6 instead of 3, and you can now sell stock you don't have.
- *Fix:* every status change is a conditional update: `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock only if that update changed a row. Optionally, have the job claim orders with `FOR UPDATE SKIP LOCKED`.

**3. The design doesn't say what happens to an order whose charge was never sent.** If an instance crashes after step 2 commits but before step 3 runs, the order is `PENDING` and the provider has never heard of it.
- *Case:* the job asks the provider about order X and gets "not found." That is neither `PAID` nor `FAILED`, so the design gives no answer.
- *Fix:* decide between two options. The job can mark the order `FAILED` and restore stock. Or it can make the charge itself, which is safe because the order id is the idempotency key. Either way, write it down.
- *Check today:* confirm the provider lets you look up a charge by *your* idempotency key or order id. After a timeout you never receive their charge id, and the schema has no column to store one anyway. If the provider can only look up charges by its own id, step 6 doesn't work as written.

**4. Some paid customers never get a confirmation email.**
- *Case A:* the order is marked `PAID` and committed, then the process dies before the email is queued. No email is ever sent.
- *Case B:* the reconciliation job marks an order `PAID`. Nothing says it queues the email.
- *Fix:* add a small outbox table. Insert the email job in the same transaction that sets `PAID`, and have a worker send from that table. Every path to `PAID` writes this row.

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved + order inserted (one txn)
    PENDING --> PAID: charge succeeded (request or reconciliation job)
    PENDING --> FAILED: declined, or reconciliation job says failed / not found
    PAID --> [*]
    FAILED --> [*]
```

Each arrow out of `PENDING` should be one conditional transaction. The `PAID` transaction also writes the outbox row, and the `FAILED` transaction also restores the stock. Fixes 1 to 4 make that true.

## Should fix (small)

- **Scope the idempotency key per customer.** Make the unique constraint `(customer_id, idempotency_key)`, not just the key, so two customers' keys can't collide.
- **Reject a reused key with a different cart.** Return `422` if the body doesn't match the existing order. Otherwise a client bug silently returns the wrong order.
- **Handle two simultaneous requests with the same key.** The second request will fail on the unique constraint. The design should say it catches that error and returns the existing order, not a `500`.
- **Prevent deadlocks between multi-item orders.** Sort the stock updates by SKU and merge duplicate SKU lines in the cart. Two carts {A, B} and {B, A} checking out at the same moment can otherwise deadlock.
- **Explain how step 2 fits the module rules.** It writes to `stock` and `orders` in one transaction, which conflicts with "no module reads another module's tables." State that the inventory module exposes something like `reserve(tx, items)` and the orders module calls it inside the shared transaction.
- **Check the load balancer timeout and the 2-minute threshold.** The worst-case charge path is about 30 s (4 attempts × 5 s plus backoff). The load balancer's timeout must be longer than that. Also write down the rule that the job's 2-minute threshold must stay well above this window, so nobody shrinks one without the other.

## Tests to add to the plan

- Reconciliation job: provider says paid, failed, and not found. Check stock and email for each.
- Both instances' jobs settle the same order at once. Stock is restored exactly once.
- Crash after step 2 commits and before the charge.
- Crash after `PAID` commits. The email is still sent.
- Two concurrent requests with the same idempotency key.
- Two customers buying the last unit at the same time. The current test buys the last unit, but not concurrently.
- A reused key with a different cart returns `422`.

You can start on the module skeletons and the reserve-and-insert step tomorrow while you write up fixes 1 to 4. Make the provider lookup check first, because if it fails, step 6 needs redesigning.

VERDICT: READY WITH CHANGES
