**This design is close, and the core of it is sound. Before you start, write down three fixes. The biggest: if two workers settle the same order, its stock gets put back twice.** The rest can be settled while you build.

## What's already right
- **No overselling.** The stock update only succeeds if enough stock is left, so two buyers can't both get the last unit.
- **No double charges.** The order id is sent to the payment provider as the idempotency key on every retry.
- **No database transaction stays open during the payment call.** Long-held locks are the usual way checkout services fail under load, and this design avoids them.
- **Orders stuck at `PENDING` get handled.** The reconciliation job settles them, and the 30-minute alert catches any it misses.

## Fix before building

**1. Stock can be restored twice, and an order can flip between `PAID` and `FAILED`.** Steps 5 and 6 say "mark the order `FAILED` and restore the stock" without checking the order is still `PENDING`. The reconciliation job runs on both instances.
- **Case:** order X reserved 2 units and is `PENDING`. Both instances run the job, both see "declined", and both restore stock.
- **Wrong result:** stock goes up by 4.
- **Expected:** stock goes up by 2.
- **Fix:** every status change must succeed only if the order is still `PENDING`, e.g. `UPDATE orders SET status = 'FAILED' WHERE id = :id AND status = 'PENDING'`. Restore stock or queue the email only if that update changed a row. Also make the job run on one instance at a time (a Postgres advisory lock, or `FOR UPDATE SKIP LOCKED` when picking orders).

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved, order inserted
    PENDING --> PAID: charge succeeded (request or reconciliation job)
    PENDING --> FAILED: declined, or provider has no charge (request or job); stock restored
    PAID --> [*]
    FAILED --> [*]
```
`PAID` and `FAILED` are final. Every arrow has to be a conditional update like the one above.

**2. The design doesn't say what happens when the provider has no record of the charge.** If an instance crashes after step 2 commits but before step 3, the order is `PENDING` and was never charged. The job asks the provider and gets "not found". Is that `PAID` or `FAILED`? The design doesn't say.
- **Fix:** if the order is older than the job's threshold and the provider has no charge, mark it `FAILED` and restore stock, with the same conditional update as fix 1. The 2-minute threshold is safely longer than the longest request (about 5 s × 4 attempts plus backoff), so the job won't race a charge that is still in progress. Write that reasoning into the doc so nobody shortens the threshold later.
- **Also say:** the job must restore stock when it marks an order `FAILED`, and queue the email when it marks one `PAID`. Neither is stated now.

**3. Sort SKUs before reserving stock.**
- **Case:** order A has items `[sku1, sku2]` and order B has `[sku2, sku1]`. They run at the same time.
- **Wrong result:** Postgres detects a deadlock and aborts one of them, so that customer gets a `500`.
- **Expected:** both orders succeed.
- **Fix:** update stock in SKU order, and merge duplicate SKU lines in the cart first.

## Can be decided while building
- **Where do prices come from?** There's no products or prices table. If `unit_price` and `total` come from the client's cart, a client can set its own price. Prices should be looked up on the server.
- **What if the same idempotency key comes with a different cart?** Return `422` instead of the old order. Also, the unique constraint should probably be on `(customer_id, idempotency_key)`, not on the key alone.
- **The email can be lost.** If the service crashes after committing `PAID` but before queuing the email, it never goes out. If the queue is a Postgres table, add the email in the same transaction as the status change.
- **How does a client find out what happened after a `202`?** You'll need `GET /orders/{id}`.
- **Load balancer timeout.** One request can take more than 20 s. Check the load balancer's idle timeout is longer than that.
- **Schema gaps.** `stock.sku` needs a primary key, `order_items` needs a primary key and a foreign key to `orders`, and `stock.available` should have `CHECK (available >= 0)`.
- **Missing tests.** Two buyers racing for the last unit at the same time (the current test has only one buyer). The job settling a declined order once even when two copies run. The job handling "provider has no charge". A decline putting stock back. The opposite-order SKU deadlock.

## Questions for you
1. Where do prices come from: a catalog service, a table you own, or the client?
2. Is the email queue in Postgres or somewhere else? That decides how simple the fix for lost emails is.

VERDICT: READY WITH CHANGES
