**The design isn't ready to build. It has a double-charge bug and a few other problems that are cheap to fix now and expensive to fix in production.**

## Blockers

**1. Payment idempotency key allows double charges (step 3).**
- **The bug:** the key is `<order id>-<attempt number>`, so each retry sends a different key. The provider treats each as a new charge.
- **Failing case:** attempt 1 charges the card but the response times out at 5 s. Attempt 2 goes out with key `ord123-2`, and the customer is charged twice.
- **Expected:** every attempt for the same order sends the same key, such as `<order id>`. The provider then returns the original result. Put the attempt number in the logs, not the key.
- This also makes the reconciliation job in step 6 unsafe, because it can race with a retry.

**2. Idempotency keys are stored per instance in memory (step 1).**
- **The bug:** there are two instances behind a load balancer. A client retries `POST /orders` with the same key, and the load balancer sends it to the other instance. That instance has never seen the key, so it creates a second order and reserves stock again.
- A restart or deploy also wipes the map.
- **Fix:** store the key in Postgres with a unique constraint, for example `orders.idempotency_key`. Insert it in the same transaction as the order. A repeat then hits the constraint and returns the existing order.

**3. Stock check can oversell (step 2).**
- **The bug:** the service reads `available` and then writes `available - quantity`. Under the default Postgres isolation level (read committed), two concurrent buyers of the last unit both read 1 and both write 0, so both orders succeed.
- The test "buying the last unit of stock" would catch this, but only if it runs concurrently. As written it probably doesn't.
- **Fix:** use a conditional update, `UPDATE stock SET available = available - $q WHERE sku = $s AND available >= $q`, and treat zero rows updated as a shortage. Alternatively, lock the rows with `SELECT ... FOR UPDATE`. Lock multiple SKUs in sorted order to avoid deadlocks.

**4. Money stored as `double precision`.**
- Both `total` and `unit_price` are floating point, which gives rounding errors on currency (0.1 + 0.2 ≠ 0.3).
- **Fix:** use integer minor units (cents) with the `currency` column, or `numeric(12,2)`. Changing this after launch means a data migration.

## Should fix before building

- **Restoring stock on a decline (step 5) isn't idempotent.** If a retry or the reconciliation job runs it twice, stock is restored twice. Guard it with a status transition: `UPDATE orders SET status='FAILED' WHERE id=$1 AND status='PENDING'`, and restore stock only if one row changed. Apply the same rule to `PAID`, so the reconciliation job and the request path can't both settle an order.
- **Orders are stuck `PENDING` with stock held.** Step 6 settles unknown results, but the design says nothing about what happens if the provider can't give a status for a long time. The alert at 30 minutes tells someone, but there's no runbook and no expiry policy. State what happens at, say, 24 hours.
- **The email can be lost (step 4).** Marking the order `PAID` and then queuing the email are two separate steps. A crash between them means the order is paid and no email is sent. Write an outbox row in the same transaction as the `PAID` update, and have a worker send from it.
- **Missing constraints and indexes:**
  - `stock.sku` needs a primary key.
  - `order_items` needs a foreign key to `orders` and a `CHECK (quantity > 0)`.
  - `stock` needs `CHECK (available >= 0)`, which acts as a safety net for problem 3.
  - Add an index on `(status, created_at)` for the reconciliation query.
- **The retry budget blocks the request.** 3 retries at 5 s plus backoff can hold the HTTP request open for 20 s or more. State the client-facing timeout, or return `202` sooner and let the background job finish the charge.
- **The reconciliation job runs on both instances.** Two instances means two jobs every 5 minutes, which can settle the same order twice. Use a Postgres advisory lock, or `SELECT ... FOR UPDATE SKIP LOCKED`.

## Smaller gaps
- Add an `UPDATE` trigger or application rule for `updated_at`.
- Say whether the cart sends prices or the server looks them up. If the client sends them, it can set its own price.
- Add authentication and the rule that `customer_id` comes from the session, not the request body.
- Add tests for: concurrent last-unit purchases, a retry that gets the same payment key, a duplicate request routed to a different instance, a double decline settlement, and a crash between `PAID` and the email.

## What's good
- Keeping the charge outside the database transaction is the right call.
- The `PENDING` state, the reconciliation job, and the stuck-order alert show you've thought about unknown payment results.
- The module boundaries and the list of integration tests are sound.
- At 2,000 orders a day, one Postgres database and two instances is plenty, so you don't need to add infrastructure.

Fixing the four blockers is about a day of design edits. The "should fix" items can go in the same revision, since several of them (the status guard, the outbox) shape the schema.

VERDICT: NOT READY
