# Review of design.md

The structure is sound and the scale assumptions are fine. 2,000 orders a day is light work for a single Postgres. The checkout flow itself, though, has two money-handling bugs and an overselling gap. All three are fixable in a short revision of steps 3–6, but fix them on paper before you write `OrderCheckout`.

## Must fix before building

**1. Retrying the charge can bill a customer twice (step 4).**
A timeout doesn't mean the charge failed. The provider may have charged the card and the response got lost. With up to 3 retries, one customer could be charged as many as 4 times.
- **Fix:** Send an idempotency key with every charge attempt, such as the order id. Most providers support this. If yours doesn't, don't retry blindly: check the charge's status with the provider before trying again.

**2. The payment call happens inside an open database transaction (steps 3–6).**
- **Charged but no order:** if the charge succeeds and the commit then fails, the customer has paid and you have no record of the order.
- **Records disappear:** on failure the PENDING order is rolled back. If the "failure" was really a timeout on a charge that went through, the order you'd need to investigate no longer exists.
- **Tied-up connections:** with a 5 s timeout and 3 retries, one request can hold a connection and transaction open for about 20 s. With a few slow payment calls at once, you run out of database connections.
- **Fix:** Commit the PENDING order *before* calling the provider. Then update it to PAID or FAILED in a second transaction. Add a periodic reconciliation job that checks orders stuck in PENDING against the provider.

**3. Nothing stops overselling (step 2).**
Checking stock against a cache up to 5 minutes old is a reasonable read-path tradeoff. But the design never reserves or decrements stock, so two buyers can both get the last item.
- **Fix:** Reserve stock atomically in your own database when the order is created. If you choose to accept overselling instead, write down what happens next (refund, backorder, or apology email) and who handles it.

## Should fix

**4. A client retry of `POST /orders` creates a duplicate order.**
Network glitches and double-clicks will happen. Accept an `Idempotency-Key` header from the client and store it on the order.

**5. Email failure handling isn't specified (step 5).**
Sending after the commit is the right order. Still, decide what happens if the email fails, so it doesn't turn a paid order into an error response. An outbox table or a background job handles this cleanly.

**6. One happy-path integration test isn't enough for a payment flow.**
Add integration tests for:
- charge timeout followed by a retry
- charge success followed by a database failure
- provider decline
- duplicate client submission

**7. Nightly backups mean you could lose up to a day of orders.**
Those are records of real payments. Turn on point-in-time recovery (WAL archiving, or your managed provider's PITR) before launch.

## Fine as is
- Module boundaries with no reading of another module's tables, thin routes, and a single `OrderCheckout` class.
- Structured logging with the order id on every line. This will be very useful for the reconciliation job.
- The reasoning for accepting the cache over the 800 ms warehouse call, as long as it's paired with a reservation (item 3).

You can start on the module scaffolding, inventory cache, and logging tomorrow. Revise steps 3–6 for items 1–3 before writing the payment code.

VERDICT: READY WITH CHANGES
