The design's overall shape is sound and you can start tomorrow, but four specific points will cause double charges, duplicate orders, overselling, or wrong totals if built as written. Each has a small, local fix, and none changes the architecture.

## Must fix before building

**1. Payment retries can charge the customer twice (step 3).**
The key sent to the provider is `<order id>-<attempt number>`, so every retry looks like a new charge to the provider.
- **Failing case:** attempt 1 times out at 5 s but the provider actually took the money. Attempt 2 goes out with key `…-2`, and the provider charges again.
- **Expected:** the provider recognises the retry and returns the first result.
- **Fix:** use one stable key per order (the order id). Log the attempt number on your side for tracing.

**2. A duplicate `POST /orders` can create two orders (step 1).**
Each instance keeps seen keys in its own in-memory map, and there are two instances.
- **Failing case:** the client's first request goes to instance A, the client retries, and the load balancer sends the retry to instance B. B has never seen the key, so it creates a second order and reserves stock again. A restart of A loses its keys in the same way.
- **Expected:** the retry returns the original order.
- **Fix:** add an `idempotency_key` column to `orders` with a unique constraint, and insert it in the step 2 transaction. On conflict, return the existing order.

**3. Two buyers can both get the last unit (step 2).**
The service reads `available`, then writes `available - quantity` computed from that read. Postgres's default isolation does not stop two transactions doing this at once.
- **Failing case:** `available = 1`, and two requests both read 1, both pass the check and both write 0. Two orders exist for one unit.
- **Expected:** one succeeds and the other gets `409`.
- **Fix:** do the check and decrement in one statement: `UPDATE stock SET available = available - $q WHERE sku = $sku AND available >= $q`. Treat zero rows updated as short stock. Add `CHECK (available >= 0)` as a backstop.

**4. Money is stored as `double precision`.**
- **Failing case:** three items at 0.10 sum to 0.30000000000000004, so the order total no longer matches the amount the provider charged.
- **Fix:** use `numeric(12,2)` or integer minor units for `total` and `unit_price`.

## Should fix, can be done during the build

- **Reconciliation can restore stock twice (steps 5 and 6).** The job runs on both instances, so both can mark the same order `FAILED` and each add the stock back. Make the transition conditional (`UPDATE orders SET status = 'FAILED' WHERE id = $1 AND status = 'PENDING'`) and restore stock only if that updated a row. Do the same for `PAID`.
- **The "provider has no record of this charge" case is undefined (step 6).** This happens if an instance crashes after step 2 commits but before the charge call. State that the job marks the order `FAILED` and releases stock; otherwise it stays `PENDING` and holds stock until the 30-minute alert.
- **Orders settled by reconciliation get no confirmation email.** Step 4 queues the email only on the request path.
- **The client has no stated way to learn the outcome after a `202`.** Name the endpoint, presumably `GET /orders/{id}`.
- **The data model lists no primary keys, foreign keys or provider charge id.** Store the charge id on the order, as you will need it for refunds and support.

## Test plan gaps

The listed tests would pass against the design as written, because they exercise one instance and one request at a time. Add:
- Two concurrent requests for the last unit: exactly one succeeds.
- Timeout where the fake provider did take the charge, then retry: exactly one charge recorded.
- Duplicate `POST /orders` across two service instances sharing a database.
- Reconciliation run twice on the same declined order: stock restored once.
- Reconciliation when the provider has no record of the charge.

## What is already right

- The charge happens outside the database transaction.
- An unknown payment result leaves the order `PENDING` and returns `202` rather than guessing.
- Email failure never fails the order.
- There is an alert on stuck orders.
- One Postgres database and modules in a single service suit 2,000 orders a day and a team of four; nothing here needs a queue or more infrastructure.

VERDICT: READY WITH CHANGES
