> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Order management

> A Blueprint for an order service that stays correct on money, stock, and customer trust while many requests compete for the same rows, payment providers retry callbacks, and traffic jumps during a sale.

> [!NOTE]
>
> **Blueprint** · **Works on:** Cockroach Continuum, CockroachDB Cloud, self-hosted · **Verified on:** CockroachDB v26.2.8 · **Code:** [`blueprints/order-management`](https://github.com/cockroachlabs/cockroach-cookbook/tree/blueprints/order-management/v1.0.0/blueprints/order-management) v1.0.0

## What you'll build

An order service where placing an order is one `SERIALIZABLE` transaction. It claims the caller's idempotency key, reserves stock with a guarded update, records the order and its audit event, and writes the outbox message that tells downstream systems. Payment callbacks, fulfillment, returns, and cancellation follow the same rules.

The reference implementation's tests are the acceptance criteria:

* 50 buyers competing for the last unit get exactly one order.
* A retried request with the same idempotency key creates nothing new.
* An order that can't reserve every line holds nothing.
* A capture that arrives before its authorization still counts.
* A capture that arrives after cancellation requests exactly one refund and ignores its duplicate.

## Get the code

The code is in the [Cockroach Cookbook](https://github.com/cockroachlabs/cockroach-cookbook) repository. To fetch only this Blueprint:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
git clone --filter=blob:none --sparse https://github.com/cockroachlabs/cockroach-cookbook.git
cd cockroach-cookbook
git sparse-checkout set blueprints/order-management
git checkout blueprints/order-management/v1.0.0
```

| Directory | What it is |
| - | - |
| [`reference/`](https://github.com/cockroachlabs/cockroach-cookbook/tree/blueprints/order-management/v1.0.0/blueprints/order-management/reference) | The reference implementation in Python (psycopg 3): the schema, the transaction handlers, 12 regression tests, and the accounting checks |
| [`agent-demo/`](https://github.com/cockroachlabs/cockroach-cookbook/tree/blueprints/order-management/v1.0.0/blueprints/order-management/agent-demo) | A small local web app (Node.js) that connects to your cluster and runs four correctness scenarios live |
| [`SKILL.md`](https://github.com/cockroachlabs/cockroach-cookbook/blob/blueprints/order-management/v1.0.0/blueprints/order-management/SKILL.md) | The agent skill: give it to your coding agent to build or extend the service against your cluster |

## Run it

You need CockroachDB v26.2.7 or later and Python 3.10 or later.

> [tab] Cockroach Continuum
>
> Create a cluster by following the [Continuum quickstart](https://docs.cockroachlabs.com/docs/continuum/tutorial-quickstart.md), create a SQL user, and run `CREATE DATABASE ordermgmt;`. Apply `reference/schema.sql` and `reference/seed.sql`, then set `DATABASE_URL` to the cluster's connection string with `/ordermgmt` as the database and `sslmode=verify-full`.

> [tab] Local (self-hosted)
>
> [Install CockroachDB](https://docs.cockroachlabs.com/docs/stable/install-cockroachdb-mac.md), start a single node, and create the `ordermgmt` database. The steps are in [`reference/README.md`](https://github.com/cockroachlabs/cockroach-cookbook/blob/blueprints/order-management/v1.0.0/blueprints/order-management/reference/README.md).

Then run the tests and the accounting checks as described in `reference/README.md`. The tests truncate the order tables, so run them only against a database created for them.

To watch the same scenarios against your own cluster, run the demo (Node.js 20 or later):

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cd agent-demo
npm install
npm start        # http://localhost:4300
```

## Read the design

> [card] [Why CockroachDB for order management](https://docs.cockroachlabs.com/docs/cookbook/order-management/introduction.md)
>
> The workload, why it exceeds many databases' guarantees, and what keeps it correct.

> [card] [Architecture and request flow](https://docs.cockroachlabs.com/docs/cookbook/order-management/architecture.md)
>
> The end-to-end picture the rest of the guide fills in.

> [card] [Data model](https://docs.cockroachlabs.com/docs/cookbook/order-management/schema-design.md)
>
> The complete reference schema.

> [card] [Inventory and reservations](https://docs.cockroachlabs.com/docs/cookbook/order-management/inventory-and-reservations.md)
>
> Guarded stock updates, hot SKUs, and reservation deadlines.

> [card] [Payments and callbacks](https://docs.cockroachlabs.com/docs/cookbook/order-management/payments-and-callbacks.md)
>
> Callbacks that arrive late, early, or twice.

> [card] [Multi-region and scale](https://docs.cockroachlabs.com/docs/cookbook/order-management/multi-region-and-scale.md)
>
> Regional placement, survival goals, and sale-day peaks.

The full guide is in the sidebar: best practices, then build and verify, then the evaluation tasks for AI agents.

## Scope

The reference is one merchant per database, USD with an explicit currency column, one full-capture payment attempt per order, all-or-nothing initial reservations, split shipments, inspected returns, and unpaid whole-order cancellation. Backorders, partial capture, multiple payment attempts, replacement orders, and multiple stock locations are extensions it doesn't implement. [`reference/VALIDATION.md`](https://github.com/cockroachlabs/cockroach-cookbook/blob/blueprints/order-management/v1.0.0/blueprints/order-management/reference/VALIDATION.md) records what was run and on which release.

## Recipes this Blueprint uses

* [Make writes idempotent with `ON CONFLICT`](https://docs.cockroachlabs.com/docs/cookbook/idempotent-writes)
* [Find and fix transaction contention](https://docs.cockroachlabs.com/docs/cookbook/transaction-contention.md)

> [card] [Talk to an engineer about your order workload](https://www.cockroachlabs.com/contact/)
>
> Reach an engineer who knows this workload.
