You can start building tomorrow. The core of the design is sound, but three gaps need a sentence or two each in the doc first, because each can leave the stock count wrong. They all sit in the reconciliation path, the job that settles orders whose payment result is unknown. None of them changes the architecture.

**What's already solid:** the duplicate-request handling (unique idempotency key, order id sent to the provider as its idempotency key), the stock update that can't go below zero (line 23), the payment call made outside any transaction, the email queued after commit, the reconciliation job, and the 30-minute alert. This is a well-thought-out revision.

## Order lifecycle and where the gaps are

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved, order inserted
    PENDING --> PAID: charge succeeds (request or reconciliation)
    PENDING --> FAILED: declined (stock restored)
    PENDING --> FAILED: reconciliation says failed (stock restored? not stated)
    PENDING --> PENDING: provider has no record of the charge (not handled)
```

## Must fix before building reconciliation

**1. High: two runs could settle the same order twice, so stock gets added back twice.**
There are two instances (line 6) and the job runs every 5 minutes (line 37). Nothing says it runs on only one instance, or that a status change only applies if the order is still `PENDING`.
- Case: an order for the last unit of SKU A is declined. Both instances' jobs pick it up, and both mark it `FAILED` and add 1 back.
- Wrong result: `available = 2`.
- Expected: `available = 1`. Otherwise a later customer buys stock that doesn't exist.
- Fix: write every status change as `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`, and only restore stock when that update changed a row. That one rule also covers a request and the job racing each other.

**2. Medium-high: a `FAILED` result from reconciliation doesn't say it restores stock.**
Step 5 (line 33) restores stock on a decline. Step 6 (line 38) only says "settles them as PAID or FAILED". If the reconciliation code is written to the spec as it stands, every order it fails keeps its reserved stock forever. Fix: say both paths use the same "fail and release stock" operation.

**3. Medium-high: there's no rule for when the provider has no record of the charge.**
If an instance crashes after step 2 commits but before step 3 sends the charge, the order is `PENDING` and the provider has nothing to report. The design only defines PAID and FAILED, so the order sits until the 30-minute alert fires and holds its stock meanwhile. Fix: after the 2-minute cutoff, treat "charge not found" as `FAILED` and release the stock. That's safe because the request path finishes well under 2 minutes: 4 attempts × 5 s plus backoff.

## Should fix (small, fine to settle while building)

4. **Medium: tests don't cover reconciliation or concurrency.** The list on lines 49–51 is missing:
   - the job settling PAID, FAILED and not-found orders
   - two jobs or a request and a job racing on the same order (issue 1)
   - two buyers going for the last unit at the same time
   - two identical `POST`s arriving at the same time
   - a decline putting the stock back

   Given the tests-first standard, I'd add these to the doc.
5. **Medium: it doesn't say where prices come from.** The client sends "the cart" (line 19), and there's no price or catalog source in the data model. If `unit_price` or `total` comes from the client, a customer can choose their own price. State that prices are looked up on the server.
6. **Low-medium: a reused key with a different cart.** Line 21 returns the existing order for any repeated key, even if the cart is different. Either reject a mismatched body with `422`, or note that this is deliberate. Also say whether the key is unique globally or per customer.
7. **Low: two identical requests at the same moment.** The second insert fails on the unique constraint, and the code must catch that and return the existing order rather than a `500`. Worth one line in the doc.
8. **Low: possible deadlock on multi-item carts.** Order X reserves SKUs A then B while order Y reserves B then A. Postgres aborts one of them and the customer gets a `500` instead of a result. Fix: reserve SKUs in sorted order.
9. **Low: the transaction crosses modules.** Step 2 writes `stock` and `orders` in one transaction, while line 41 keeps each module to its own tables. That's workable, but name how the two modules share one transaction (for example, a shared transaction passed into both).
10. **Low: missing database constraints.** Add:
    - a primary key on `stock.sku`
    - a primary key on `order_items (order_id, sku)`
    - `CHECK (available >= 0)` and `CHECK (quantity > 0)`
    - `idempotency_key NOT NULL`
11. **Low: what gets retried.** Say that only timeouts and provider 5xx errors are retried. A decline must not be retried.
12. **Low: confirmation emails.**
    - Orders that reconciliation marks `PAID` should also queue the confirmation email.
    - If the process crashes between the commit and the enqueue (step 4, line 31), the email is lost. That's acceptable at this volume, but say so or use an outbox table.

## Worth thinking about (not shown by the doc)

- **Load balancer timeout.** In the worst case the request takes about 25–30 s. If the load balancer cuts it off first, the client gets an error instead of `202`. Retrying with the same key still works, so this is mostly about the client experience.
- **How the client learns the outcome after a `202`.** Re-sending the `POST` with the same key works today. A `GET /orders/{id}` endpoint would be cleaner.

## Suggested path

Build the happy path and the stock reservation tomorrow; neither depends on the fixes above. Before you write the reconciliation job, update the doc with issues 1–3 and the extra tests from issue 4.

VERDICT: READY WITH CHANGES
