Purpose and audience
This guide is an implementation specification for an order service: the application that accepts customer orders, holds stock, charges payment, and drives fulfillment, with CockroachDB as its system of record. A system of record is the authoritative store for a piece of data; here it is the database that owns order, inventory, and payment-intent state. The guide is written for two audiences:- Developers building or operating an order service and choosing the patterns that keep it correct under load.
- AI agents generating or reviewing such a service. Each page states its invariants, shows concrete DDL and SQL, and ends in checks an agent can run (see Verification and Evaluation tasks).
Component overview
The system has four tiers and three external systems.- Client / API layer. Accepts requests, authenticates and validates them, and forwards each call to the order service. Clients retry, so every state-changing call carries a caller-supplied idempotency key, a token that lets the server recognize a retry of the same logical request.
- Order service (application tier). Holds the business logic. It opens CockroachDB transactions, enforces state transitions, and wraps every transaction in a retry loop for transaction retry errors (
SQLSTATE 40001). It is stateless and horizontally scalable; correctness lives in the database, not in the process. - CockroachDB (system of record). A distributed SQL database that is strongly consistent and, by default, uses
SERIALIZABLEisolation. It owns the authoritative state for orders, line items, inventory, reservations, payment intent, and the audit log. It is also compatible with the PostgreSQL wire protocol, so existing drivers and tools connect to it unchanged. - External systems. The payment provider (money movement), the warehouse management system (WMS) (picking and packing), and the shipping carrier (delivery) are systems of action, reached asynchronously. The database never calls them inside a transaction. Instead, each committed change writes a row to a transactional outbox table, and a relay driven by changefeeds (CockroachDB’s change-data-capture mechanism) delivers those rows outward. Inbound callbacks are absorbed through an inbox. See External integration.
Request flow: placing an order
The following steps trace onePOST /orders request. Steps 1 to 4 execute inside a single SERIALIZABLE transaction; steps 5 to 6 happen asynchronously after commit.
- Idempotency check. The service reads the request’s idempotency key. If the key already exists and its work is complete, it returns the stored response and does nothing else. A retry produces no second order. If the key is new, it is claimed in the same transaction as the work below. See Order lifecycle and idempotency.
-
Inventory reservation (guarded). For each line item, the service issues a guarded decrement, an
UPDATEwhoseWHEREclause requires sufficient stock. If stock is insufficient, the statement matches zero rows and the order is rejected atomically, with no overselling race. See Inventory and reservations. -
Write order, lines, audit event, and outbox row, atomically. In the same transaction, the service inserts the
ordersrow, itsorder_lines, anorder_eventsaudit record, and anoutboxrow describing the downstream message. Because they commit together, state and history can never diverge, and no message is ever emitted for a change that rolled back. -
Commit (with retry). The transaction commits. Under contention it may return a transaction retry error (
SQLSTATE 40001); the service’s retry loop re-runs the whole transaction. Only after commit does the caller receive a success response. - Asynchronous payment capture. The outbox row drives a request to the payment provider, keyed by a service-generated idempotency key. The provider’s webhooks return through the inbox, which deduplicates them and advances payment state forward only. See Payments and callbacks.
- Fulfillment. Once payment is captured, further outbox events notify the WMS and carrier, and shipment and return records track the physical flow. See External integration.
[!NOTE]
Why one transaction for steps 1 to 4
Combining the idempotency claim, the guarded reservation, and the order, line, event, and outbox writes into a single SERIALIZABLE transaction is what makes the core path correct: either every effect of placing the order is recorded, or none is. Splitting these into separate transactions reintroduces the double-order, oversell, and lost-message races the rest of the guide works to eliminate.
Environmental assumptions and prerequisites
- CockroachDB v26.2.7 or later (the latest LTS release). Every pattern referenced in this guide, including server-side triggers, is available at this version. Version boundaries noted elsewhere in the guide record when a feature first shipped; confirm feature availability against your target release.
- A PostgreSQL-protocol driver. The order service connects over the PostgreSQL wire protocol using any standard driver (for example, pgx, pg, or psycopg). No CockroachDB-specific client is required.

