`design.md` is close: the core checkout flow is sound, but it needs two changes to the reconciliation job and one answer about prices before you build. All three are edits to the document, not redesigns, so you can still start tomorrow.

## Fix before building

**1. Stock can be restored twice for one failed order. Severity: high.**
- **Case:** the design does not say the reconciliation job runs on only one of the two instances. If both run it, both see order X as `PENDING` and declined, and both mark it `FAILED` and add its quantity back.
- **Result:** `stock.available` ends up one unit higher than reality, and a later customer buys a unit that does not exist.
- **Fix:** make every status change conditional (`UPDATE orders SET status = 'FAILED' WHERE id = :id AND status = 'PENDING'`) and restore stock only if that update changed a row. Apply the same rule to steps 4 and 5.

**2. An order that was never charged has no defined outcome. Severity: high.**
- **Case:** an instance crashes after step 2 commits and before the charge call in step 3 reaches the provider. The reconciliation job asks the provider about the order, and the provider answers "no such charge".
- **Result:** step 6 only covers `PAID` or `FAILED`, so the order stays `PENDING` forever, its stock stays reserved, and the 30-minute alert fires every time.
- **Fix:** state that "no charge found" settles the order as `FAILED` and restores the stock. Also state explicitly that the job restores stock whenever it settles an order as `FAILED`.

**3. The design does not say where prices come from. Severity: high if the client's are trusted, otherwise none.**
- **Gap:** the client "sends the cart", and there is no price table in the data model. If `unit_price` and `total` are taken from the request, a customer can set their own price.
- **Fix:** add one line saying the service looks prices up server-side and computes the total itself.

## Fix during the build

| # | Problem | Severity | Fix |
|---|---|---|---|
| 4 | Two concurrent orders for the same SKUs in opposite order (A then B, B then A) can deadlock in step 2; Postgres aborts one and the customer gets a 500. | Medium | Update stock rows in sorted `sku` order. |
| 5 | The test list has no cases for the reconciliation job, the crash in problem 2, two simultaneous requests with the same key, or two simultaneous buyers of the last unit. | Medium | Add these four; they cover the paths most likely to lose money or stock. |
| 6 | The idempotency key is unique across all customers, and a repeated key with a different cart silently returns the old order. | Medium | Make it unique on `(customer_id, idempotency_key)` and return `422` when the cart differs. |
| 7 | After a `202`, the design gives the client no way to learn the outcome, and a customer whose order is later settled `FAILED` is never told. | Medium | Add `GET /orders/{id}` and a failure notification. |
| 8 | The provider's charge id is not stored on the order, so support and any future refund must search by order id at the provider. | Low | Add a `payment_reference` column. |
| 9 | If the instance crashes between committing `PAID` and queueing the email in step 4, the confirmation is never sent. | Low | Have the reconciliation job also queue missing emails, or write the email request in the same transaction. |
| 10 | The data model lists no primary keys, foreign keys, or a `CHECK (available >= 0)` on `stock`. | Low | Add them to the table definitions. |
| 11 | Step 3 can hold the request open for over 20 seconds (four 5-second attempts plus backoff), which may exceed the load balancer's timeout. | Low | Check the timeout; the idempotency key already makes the client's retry safe. |

## What is already right
- Reserving stock with a conditional update inside one transaction prevents overselling.
- The charge call sits outside any transaction and reuses the order id as the provider's idempotency key, so retries cannot double-charge.
- The size of the design fits the load: 2,000 orders a day does not need queues or sagas, and the document does not add them.

VERDICT: READY WITH CHANGES
