Almost. The design is solid and you can start building tomorrow, but three gaps in how pending orders get settled need a few lines added to the doc first. Each one can leave stock counts wrong or leave an order stuck, and they're cheaper to fix now than once code exists.

## What's already strong

- **Duplicate requests are handled properly.** The idempotency key is unique, and the stock update and order insert share one transaction. If two copies of the same request race, the losing insert rolls back its own stock change.
- **Stock can't go negative.** The `available >= :qty` condition makes overselling impossible without needing locks.
- **The payment call sits outside the database transaction**, and sending the order id as the provider's idempotency key makes retries safe.
- **A failed email can't fail an order**, and there is a reconciliation job plus an alert for orders stuck in `PENDING`.

## Changes to make before building

**1. Settling an order must happen exactly once.**
You run two instances, so the reconciliation job will probably run on both. Step 6 also doesn't say that settling as `FAILED` puts the stock back.
- *Example:* a declined order for the last unit of SKU A is still `PENDING`. Both instances' jobs pick it up, and each marks it `FAILED` and adds 1 back. Stock now shows 1 unit that doesn't exist, and the next customer buys it.
- *Fix:* every settlement uses `UPDATE orders SET status = :s WHERE id = :id AND status = 'PENDING'`. Only the caller that changed exactly one row restores stock (on `FAILED`) or queues the email (on `PAID`). Apply this in steps 4 and 5 and in the job.

**2. Decide what happens when the provider has no record of the charge.**
- *Example:* the service crashes after step 2 commits but before step 3, or the first charge attempt never reaches the provider. The job asks for the charge status and gets "not found." The design doesn't say what to do next, so the order stays `PENDING` forever, holds its stock, and sets off the 30-minute alert.
- *Fix:* add a rule. For example, "not found and older than N minutes → `FAILED` and restore stock," or "retry the charge once with the same idempotency key."

**3. Reserve items in a fixed SKU order.**
- *Example:* order 1 holds [A, B] and order 2 holds [B, A], and both arrive at the same moment. Each locks its first row and waits for the other's, so Postgres kills one of them with a deadlock error.
- *Fix:* sort the SKUs before running the updates. It's one line.

Here's the order lifecycle once those changes are in:

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders reserves stock
    PENDING --> PAID: charge succeeds (request or job) → queue email
    PENDING --> FAILED: declined, or provider has no record → restore stock
    note right of PENDING: every move out of PENDING is a conditional update and runs once
```

## Smaller items (fine to settle while building)

- **Retry only on timeouts and 5xx errors, never on a decline.** Also check the load balancer's timeout: 4 attempts at 5 s each plus backoff holds the client's request open for over 25 s.
- **Clients need a way to see the final status after a `202`**, for example `GET /orders/{id}`.
- **Module boundaries:** step 2 writes the `stock` table inside the orders transaction. Have the inventory module expose a `reserve(tx, items)` function so the "no module reads another module's tables" rule still holds.
- **Known tradeoff on emails:** if the service crashes between committing `PAID` and queuing the email, that email is lost. At 200–2,000 orders a day that's acceptable. If it starts to matter, the fix is writing to an outbox table in the same transaction.
- **Tests to add:**
  - the reconciliation job settling as `PAID`, as `FAILED`, and on "not found"
  - two jobs settling the same order (stock restored only once)
  - a decline restoring stock
  - two concurrent orders with overlapping SKUs

VERDICT: READY WITH CHANGES
