Commerce architecture
What you need to know
A commerce application separates browseable catalog content from trusted checkout and durable order state. Next.js can serve both surfaces, but prices, permissions and order effects belong to server-side contracts.
By the end, you can: separate catalog and checkout; plan idempotent mock operations.
Compute a bounded server-side quote
export function quoteCents(unitCents:number, quantity:number){ if(!Number.isSafeInteger(unitCents)||unitCents<0||unitCents>1000000||!Number.isInteger(quantity)||quantity<1||quantity>20) return null; return unitCents*quantity;}Expected behavior: quoteCents(1250, 3) returns 3750. Unit price must come from the trusted catalog, not request JSON.
How it works
A public catalog can display cached descriptive information, but a checkout must re-read authoritative prices, stock and actor entitlement at the operation boundary. Use integer minor units with an explicit currency and bounded values; avoid treating a client-submitted total as a price. Persist the order and an idempotency record in the same transaction. An idempotency key identifies an operation attempt, scoped to the actor and operation, and its stored fingerprint distinguishes an identical retry from a conflicting reuse. For webhook processing in a real provider integration, verify authenticity over the provider-defined raw request bytes using its maintained SDK and required replay controls, then validate event shape, record the provider event ID atomically with the effect and tolerate duplicate delivery. This is a mock order design; no payment credentials, purchases or external calls. Acknowledge only according to the provider's retry contract. An outbox can deliver post-commit invalidation or downstream work; process retries idempotently and bound queue processing. A worker can crash after a downstream effect succeeds but before recording completion, so an outbox alone does not promise exactly-once delivery. Use a durable event ID and downstream deduplication where supported. Catalog cache freshness cannot substitute for order integrity.
Idempotency decision worksheet
[ { "key": "attempt-1", "fingerprint": "book-a:2", "existing": null, "decision": "create" }, { "key": "attempt-1", "fingerprint": "book-a:2", "existing": "book-a:2", "decision": "replay" }, { "key": "attempt-1", "fingerprint": "book-a:3", "existing": "book-a:2", "decision": "conflict" }]Expected behavior: The same key and payload replay the original outcome; a changed payload must not silently create another order.
Common mistake
Never charge or decrement stock based only on a client total or browser-generated event. A pre-insert lookup without a database uniqueness guarantee can race.
Practice and verification
Use the exercise for idempotent progress events as an analogous pure model. Explain how a durable unique constraint and transaction would implement the same contract for checkout. Test duplicate and conflicting requests.
Scope: Proposed Next.js 16.x / React 19; App Router, Cache Components disabled unless noted. Framework and browser examples are staged, not executed. No exact patched dependency release or live deployment verified.