## Is design.md ready to build from?

Yes, with changes. The core of the design is sound: it reserves stock and creates the order in one transaction, keeps the payment call outside any transaction, uses the order id as the payment idempotency key, and has a reconciliation job for unknown results. You don't need to redesign anything. But there is one high-severity bug (stock can be restored twice) and a few gaps that will cause real defects if you build from the document as written. Each fix is a sentence or two in the doc, so you can make them tonight and still start tomorrow.

## Problems, by severity

**HIGH: Stock can be restored twice when an order fails.**
Both service instances run the reconciliation job, and nothing stops them from settling the same order at the same time. Nothing stops the job and a live request from racing each other either.
- **Example:** order X holds 1 unit of SKU `A`, and `A` now shows `available = 4`. At 12:05 both instances run the job and the provider says "declined." Both mark X `FAILED` and both add 1 back.
- **Result:** `available = 6`. It should be 5, so you can sell a unit you don't have.
- **Fix:** every status change must be a conditional update, `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock only when that update actually changes a row. Also have the job claim its orders with `FOR UPDATE SKIP LOCKED` or a Postgres advisory lock, so only one instance works on a given order.

**MEDIUM: The reconciliation job doesn't restore stock when it settles an order as `FAILED`.**
Step 5 restores stock on a decline, but step 6 doesn't say the job does the same.
- **Fix:** the job must use the same "mark failed and restore stock" step as step 5.

**MEDIUM: The reconciliation job has no rule for "the provider has no record of this charge."**
That happens when every attempt timed out before reaching the provider. As written, the order stays `PENDING` forever, holds its stock, and sets off the 30-minute alert.
- **Fix:** decide on one rule. Either "not found" means `FAILED` plus restoring stock, or the job retries the charge with the same idempotency key.

**MEDIUM: The idempotency key is unique across all customers.**
- **Example:** customer B's client sends a key that customer A already used. B gets A's order back, which leaks A's data, and B's order is never created.
- **Fix:** make the unique constraint `(customer_id, idempotency_key)`. Also reject a reused key that comes with a different cart (return `422`).

**MEDIUM: A client that gets a `202` has no way to find out how the order ended.**
- **Fix:** add `GET /orders/{id}` to the design, or state how the client is told the result.

**MEDIUM: The design doesn't say where prices come from.**
The client sends "the cart." If `unit_price` or `total` is taken from that request, a client can set its own price.
- **Fix:** state that prices are looked up on the server from the catalog.

**MEDIUM: The tests miss the riskiest paths.**
Add tests for:
- the job settling an order as `PAID`, as `FAILED`, and as "not found"
- two job runs settling the same order at once (this is the stock bug above)
- the job racing a live request
- two `POST /orders` with the same key arriving at the same time
- the process crashing after a successful charge but before the order is marked `PAID`
- a decline that comes after a timeout

**LOW: The module rule conflicts with step 2.**
The design says no module reads another module's tables, yet step 2 writes `stock` and `orders` in one transaction. Say how this works, for example: `OrderCheckout` opens the transaction and passes it to the inventory and orders modules. Otherwise one module will end up writing the other's table directly.

**LOW: Orders settled by the job get no confirmation email.**
Steps 4 and 6 say nothing about it. Also, if the process crashes between committing `PAID` and queuing the email, that email is lost. An outbox table (writing the email job in the same transaction as the status change) fixes both. If you skip it at this volume, write that down as a known gap.

**LOW: Concurrent checkouts can deadlock.**
Orders for `[A, B]` and `[B, A]` running at the same time can lock each other. The fix is to update stock rows in sorted SKU order.

**LOW: The data model is missing a few constraints and indexes.**
Add a `CHECK (available >= 0)` constraint and primary keys on `stock(sku)` and `order_items`. The job also needs an index on `orders(status, created_at)` to find old `PENDING` orders.

**LOW: The retry rules are loose.**
Say that the service retries only on timeouts or provider errors, never on a decline. Also note that the worst case (4 attempts at 5 s each, plus backoff) is about 25–30 s, which has to fit inside the load balancer's request timeout.

## Order status with the fixes

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved + order inserted (one transaction)
    PENDING --> PAID: charge succeeded (request or reconciliation job)
    PENDING --> FAILED: declined, or provider has no record (request or job)
    FAILED --> [*]: stock restored only if the status update changed a row
    PAID --> [*]: confirmation email queued
```

Every arrow out of `PENDING` should be the conditional update described under the first problem, whether a live request or the reconciliation job makes the change.

VERDICT: READY WITH CHANGES
