The design is mostly sound and you can start tomorrow, but first add about five sentences to it. The two most serious gaps can lose stock or money:

- **Settling orders twice:** nothing stops an order from being marked `FAILED` twice, and each time puts its stock back.
- **Orders with no charge:** the design doesn't say what happens to a `PENDING` order the payment provider has never heard of.

Neither needs a redesign. Both are rules you'd otherwise end up deciding halfway through coding.

## What already works
- **Stock can't oversell.** Reserving stock with a conditional `UPDATE` inside the order transaction means two buyers can't both get the last unit.
- **The payment call is in the right place.** It runs outside any transaction and reuses the order id as the key on every retry, so a retry can't charge twice.
- **Unknown results are covered.** An unclear charge result leaves the order `PENDING`, the 5-minute reconciliation job settles it, and an alert fires after 30 minutes stuck.
- **Email can't break an order.** The confirmation email is queued after the commit and never fails the order.
- **The size fits.** One Postgres and two instances is plenty for 2,000 orders a day.

## Problems, by severity

The fixes below are about guarding the order status changes, so here is the lifecycle with each guard:

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded<br/>(only if still PENDING)
    PENDING --> FAILED: declined, or provider has no charge<br/>(only if still PENDING; then restore stock)
    PAID --> [*]
    FAILED --> [*]
```

**High**

1. **Stock can be restored twice.** Both service instances can run the reconciliation job at the same moment, and nothing says a status change has to check the current status first.
   - *Example:* order X (2 units of SKU A) is declined. Instance 1 and instance 2 both pick it up as `PENDING`. Both mark it `FAILED` and both add 2 units back, so SKU A ends up 2 units higher than it really is.
   - *Fix:* make every status change conditional, e.g. `UPDATE orders SET status = 'FAILED' WHERE id = :id AND status = 'PENDING'`, and restore stock only if that update changed one row. Also make the job run on one instance only (a Postgres advisory lock or `SELECT … FOR UPDATE SKIP LOCKED`). The same check protects against the request and the job settling the same order at once.

2. **The design doesn't cover a crash between saving the order and charging it.** If an instance dies after step 2 commits but before step 3 sends the charge, the order is `PENDING` with its stock held, and the provider has never seen it. The job asks the provider and gets "not found", and the design only says it settles orders as `PAID` or `FAILED`.
   - *Fix:* write down the rule. "Not found" and older than the longest possible retry window means `FAILED` plus stock restored. "Still processing" means leave it for the next run.
   - The longest retry window is 4 attempts × 5 s plus backoff, roughly 35 s. That is under the 2-minute threshold, so the rule is safe. Say so in the design.

3. **The design doesn't say where prices come from.** The client sends "the cart", and `unit_price` and `total` are stored, but there is no price or catalog table. If prices come from the client, anyone can buy at any price they like.
   - *Fix:* state that the server looks up prices and computes the total, and name the source.

**Medium**

4. **An idempotency key reused with a different cart isn't handled.** Today the same key with a different cart quietly returns the old order. The usual behaviour is to reject the mismatch with `422`.
   - *Fix:* store a hash of the request body alongside the key and compare it on repeats.
   - Also scope the unique constraint to `(customer_id, idempotency_key)`. With a global constraint, one customer sending another customer's key would get that customer's order back.

5. **Quantities aren't validated.** A negative `quantity` turns the reservation `UPDATE` into a stock increase: `available - (-5)` adds 5.
   - *Fix:* require `quantity > 0` at the API, and add a `CHECK (available >= 0)` on `stock` as a safety net.

6. **A client that gets `202` can't find out what happened.** There is no way to read an order's status.
   - *Fix:* add `GET /orders/{id}` to the design, or say how the client gets the final result.

7. **The confirmation email can be lost.** If the instance crashes after the `PAID` commit but before the email is queued, the email never goes out.
   - *Fix:* write a row to an outbox table in the same transaction as the `PAID` update. A separate job reads the outbox and sends the email. If a missed email is acceptable, write that down as a known gap instead.

8. **Tests are missing for the dangerous paths.** Add:
   - two requests with the same key arriving at the same time
   - two buyers going for the last unit at the same time (the current test has only one buyer)
   - reconciliation outcomes: charge succeeded, declined, not found, and still processing
   - two reconciliation runs at once, checking stock is restored only once
   - a decline puts the stock back
   - a failed email doesn't affect the order

**Low**

9. **Possible deadlocks on multi-item orders.** Order A reserves SKUs [x, y] while order B reserves [y, x]. Postgres detects the deadlock and aborts one of them, which surfaces as a `500`.
   - *Fix:* reserve SKUs in sorted order.
10. **Requests can run long.** A request can take about 35 s across retries. Check that the load balancer timeout is longer than that. If it's shorter, the client sees an error while the charge may still succeed. Its retry with the same key is safe, but confusing.
11. **The data model is incomplete.**
    - Primary and foreign keys aren't listed. Use `stock.sku` as the PK, a composite PK on `order_items`, and an FK from `order_items` to `orders`.
    - There's no column for the provider's charge id. You'll want it for refunds and support.
12. **Logic split across modules:** the "mark `FAILED` + restore stock in one transaction" step touches both the orders and the inventory module. Decide whether `OrderCheckout` coordinates one shared transaction through both modules' interfaces. Otherwise someone will break the rule that no module reads another module's tables.
13. **Authentication isn't mentioned.** Say how `customer_id` is established. If it comes from the request body, anyone can place orders as someone else.

## What to do before building
Add the fixes for items 1–3 to the design, which is a few sentences each. Decide items 4–7 in the same pass. Items 8–13 can be handled while you build. With the design written this way, the TDD list in item 8 tells you what to build first.

VERDICT: READY WITH CHANGES
