> ## 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.

# Multi-region & scale

> This page extends the single-region order system to run close to users in multiple geographies, survive the loss of an entire region, and absorb flash-sale traffic.

This page extends the single-region order system to run close to users in multiple geographies, survive the loss of an entire region, and absorb flash-sale traffic. Each rule below states the recommendation, the reason it holds, the DDL/SQL, and both sides of the tradeoff. The entities are the same throughout: `orders`, `order_lines`, `inventory`, `reservations`.

## Get single-region correctness first: multi-region is an extension

**Rule.** Do not reach for multi-region until your single-region system is correct.

**Why.** Multi-region changes *where* data lives and *how far* a quorum must travel. It does not change the correctness primitives: the guarded decrement, idempotent order creation, and guarded state transitions ([Transactions & SQL](https://docs.cockroachlabs.com/docs/cookbook/order-management/transactions-and-sql.md)), and the counter/ledger inventory model ([Schema design](https://docs.cockroachlabs.com/docs/cookbook/order-management/schema-design.md)). If overselling is possible in one region, spreading the data across three regions makes it *more* likely, not less, because latency widens the window for contention. Multi-region is a deployment property layered on a correct schema, not a substitute for one.

**When it is required.** Three drivers, and only these:

* **Latency near users.** Home each customer's rows in the region they read and write from.
* **Data residency.** Keep EU customer rows physically in the EU, US rows in the US.
* **Region-failure survival.** Continue accepting orders when an entire cloud region becomes unavailable.

If none of these apply, stay single-region. See the [Multi-region overview](https://docs.cockroachlabs.com/docs/stable/multiregion-overview.md).

> [!NOTE]
>
> **Enterprise licensing: NEEDS VERIFICATION**
> Multi-region SQL (table localities, survival goals) and bounded-staleness follower reads are gated behind the CockroachDB Enterprise license under the current CockroachDB Software License. Confirm your entitlement before designing around these features: [Licensing FAQs](https://docs.cockroachlabs.com/docs/stable/licensing-faqs.md).

## "Multi-region" is not automatically "active-active"

Two or three regions in a cluster do **not** mean every region accepts every write at local latency. Before describing a design as active-active, specify three things:

* **Write ownership**: which region owns each row's writes (its `REGIONAL BY ROW` home), so that the normal write path is *local* rather than paying cross-region consensus on every transaction.
* **Read behavior**: which reads must be fresh (leaseholder) and which tolerate staleness (follower reads).
* **Failover**: what happens on a region loss, and whether the latency and consistency behavior has been *validated* rather than assumed.

A design in which one region owns a row's writes and other regions read it is a sound architecture; describe it precisely. Where the consistency boundary allows, **batch** writes to reduce per-operation cross-region overhead.

## Choose a table locality per access pattern

**Rule.** Pick the locality that matches each table's read/write geography. The three localities are [REGIONAL BY TABLE, REGIONAL BY ROW, and GLOBAL](https://docs.cockroachlabs.com/docs/stable/table-localities.md).

First declare the database's regions:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE commerce PRIMARY REGION "us-east1";
ALTER DATABASE commerce ADD REGION "europe-west1";
ALTER DATABASE commerce ADD REGION "asia-south1";
```

### REGIONAL BY TABLE: one home region for the whole table

Fast reads and writes in a *single* region; other regions pay cross-region latency. Use it when a table is owned by one region (for example, a region-local operations table).

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE reservations SET LOCALITY REGIONAL BY TABLE IN "us-east1";
```

**Tradeoff.** Lowest latency for the home region and highest for all others. Unsuitable for data whose users are spread across regions.

### REGIONAL BY ROW: per-row home via `crdb_region`

Each row carries a hidden `crdb_region` column that determines where it lives. Home each order and customer near the user who owns it. This is the primary locality for order data.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE orders SET LOCALITY REGIONAL BY ROW;
ALTER TABLE order_lines SET LOCALITY REGIONAL BY ROW;

-- Pin a row explicitly, or let it default from the gateway region:
INSERT INTO orders (id, customer_id, status, currency, total, crdb_region)
VALUES (gen_random_uuid(), $customer_id, 'created', 'EUR', 0, 'europe-west1');
```

**Tradeoff.** Local-speed reads and writes for rows homed in the caller's region; cross-region access to a row homed elsewhere still pays the WAN cost. It also satisfies data residency: EU rows with `crdb_region = 'europe-west1'` remain in the EU.

### GLOBAL: low-latency reads everywhere, slower writes

Every region reads locally at the cost of higher write latency. Suited to read-mostly reference data: product catalog, tax tables, currency reference.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE tax_rates SET LOCALITY GLOBAL;
```

> [!CAUTION]
>
> **GLOBAL is wrong for inventory counters**
> `GLOBAL` optimizes reads by making writes globally coordinated and slower. The oversell-critical `inventory.available` counter is write-heavy and contended, which is the workload `GLOBAL` penalizes. Keep `inventory` `REGIONAL BY ROW` (or `REGIONAL BY TABLE` where stock is region-local) so that the decrement remains a fast, local leaseholder write.

## Pick a survival goal deliberately

**Rule.** Set survival to [ZONE or REGION](https://docs.cockroachlabs.com/docs/stable/multiregion-survival-goals.md) based on what failure you must survive, not by default.

* **ZONE (default).** Survives losing nodes/zones within a region. Lower write latency because quorum can stay inside one region.
* **REGION.** Survives the loss of an *entire* region. Requires **at least 3 regions**, and costs write latency because every write quorum must span regions.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- Survive a full region outage (needs >= 3 regions):
ALTER DATABASE commerce SURVIVE REGION FAILURE;

-- Or the lower-latency default:
ALTER DATABASE commerce SURVIVE ZONE FAILURE;
```

**Tradeoff.** `SURVIVE REGION FAILURE` provides continuous order-taking through a region outage, but every write then waits on a quorum acknowledgment from a *remote* region. The added commit latency is unavoidable: it is the physical cost of durability across regions. Choose REGION only when a region outage is a business-critical event that must be survived; otherwise ZONE keeps writes fast.

## Scale reads with follower reads: never the oversell decrement

**Rule.** Serve high-volume, staleness-tolerant reads from the nearest replica with [follower reads](https://docs.cockroachlabs.com/docs/stable/follower-reads.md). Order history and availability *display* qualify. The inventory decrement does not.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- Exact-staleness follower read (~4.2s or more stale), read-only:
SELECT id, status, total, created_at
  FROM orders AS OF SYSTEM TIME follower_read_timestamp()
 WHERE customer_id = $customer_id;

-- Bounded-staleness: single-statement, single-row, no index join:
SELECT available
  FROM inventory
  AS OF SYSTEM TIME with_max_staleness('10s')
 WHERE sku = $sku;
```

Follower reads scale reads horizontally: any replica can serve them, so they do not accumulate on the leaseholder. Bounded staleness (`with_max_staleness()` / `with_min_timestamp()`) is restricted to single-statement, single-row, read-only queries with no index join.

> [!CAUTION]
>
> **Never serve the oversell decrement from a stale read**
> The `UPDATE inventory SET available = available - $qty WHERE available >= $qty` must execute against the **fresh leaseholder write path**. A follower read is stale by design (roughly 4.2s or more for exact staleness). Deciding "in stock" from stale data and then writing reintroduces the exact race the guarded decrement exists to prevent, and will cause overselling. Follower reads are read-only and for *display* only. The decrement is a fresh leaseholder write.

## Flash sales & hot SKUs at scale

**Rule.** Combine every single-region mitigation, then offload display reads to followers and size the cluster for the spike.

* **Guarded decrement** rejects oversell at `0 rows affected` ([Transactions & SQL](https://docs.cockroachlabs.com/docs/cookbook/order-management/transactions-and-sql.md)).
* **`SELECT ... FOR UPDATE`** serializes writers on a hot SKU cleanly ([SELECT FOR UPDATE](https://docs.cockroachlabs.com/docs/stable/select-for-update.md)).
* **Counter sharding** splits `inventory.available` into N sub-rows summed on read, spreading the write hotspot across ranges.
* **Tiny, single-batch transactions** minimize the contention window.
* **`READ COMMITTED` with guards** reduces retries where the `WHERE` guard and `FOR UPDATE` make it safe; keep the guards, because `READ COMMITTED` permits lost updates without them.
* **Offload availability-display reads to follower reads**, keeping the leaseholder free for decrements.

**Capacity.** Hot rows and ranges are addressed by load-based splitting and by adding nodes to scale throughput; see [Performance best practices](https://docs.cockroachlabs.com/docs/stable/performance-best-practices-overview.md). Load-test at roughly **2x expected peak** so that headroom is proven before the sale rather than discovered during it.

**Tradeoff.** Sharding and `FOR UPDATE` add code and read-time aggregation; the benefit is surviving a SKU that would otherwise serialize the entire sale onto one hot range.

## Data residency and compliance

Some order data is subject to data-residency or data-sovereignty requirements. A regulation may require that a customer's personal data, or an order's records, be stored and processed within a specific country or region. Responding to this by running a separate database per jurisdiction creates data silos, duplicates operational work, and removes any single view of inventory and orders.

CockroachDB enforces residency within one multi-region cluster. `REGIONAL BY ROW` assigns each row a home region through the hidden `crdb_region` column, so an order or customer record is stored and served from the region its residency rules require while the cluster remains a single logical database.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE ordermgmt SET PRIMARY REGION "eu-west1";
ALTER DATABASE ordermgmt ADD REGION "us-east1";
ALTER TABLE orders SET LOCALITY REGIONAL BY ROW;
```

Determine each row's home region from the data itself rather than from where the write originates. Set `crdb_region` explicitly on insert, or derive it from an application column such as the order's jurisdiction, so that a record governed by a given regulation is always homed in the corresponding region.

Apply localities according to residency scope:

* Keep jurisdiction-bound tables `REGIONAL BY ROW`, or `REGIONAL BY TABLE` where an entire table belongs to one region.
* Keep only non-regulated, read-mostly reference data (for example, a product catalog) `GLOBAL`. Rows in a `GLOBAL` table are replicated to every region, so do not place regulated records there.

For stricter isolation (for example, confining a set of regions' data to a defined subset of nodes), review **super regions** and data placement controls.

> [!NOTE]
>
> **Verify residency guarantees**
> Data residency and sovereignty are legal obligations. Confirm the specific regulation's requirements, the regions the cluster spans, and the current CockroachDB multi-region, placement, and licensing behavior before relying on this configuration for compliance.

The same mechanism, binding each record's storage to a required region within one cluster, supports any workload that must keep a jurisdiction's data in that jurisdiction.

## Observability & disaster recovery

**Rule.** Monitor CDC health and contention continuously; treat multi-region survival and backups as *complementary* layers of disaster recovery (DR).

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW CHANGEFEED JOBS;   -- CDC pipeline health: running, paused, failed, retrying
```

Monitor changefeed job status for the outbox/CDC pipeline ([Change data capture overview](https://docs.cockroachlabs.com/docs/stable/change-data-capture-overview.md)) alongside standard contention and hot-range metrics.

**DR has two layers.** Multi-region **survival goals** provide in-cluster high availability: the cluster keeps serving through a region loss. **Backups and point-in-time recovery (PITR)** are a separate, complementary layer protecting against logical corruption and operator error. Survival does not replace backups, and backups do not provide zero-downtime failover. Backup and PITR specifics are out of scope here; see [Change data capture overview](https://docs.cockroachlabs.com/docs/stable/change-data-capture-overview.md) and the multi-region documentation for the survival side.

## Tradeoffs summary

| Decision | Option A | Option B | Guidance |
| - | - | - | - |
| **When to go multi-region** | Stay single-region (simpler, correct first) | Multi-region (latency, residency, survival) | Single-region until one of the three drivers applies |
| **Locality: order data** | REGIONAL BY TABLE (one home) | REGIONAL BY ROW (per-row home) | REGIONAL BY ROW for `orders`/`order_lines`/customers |
| **Locality: reference data** | GLOBAL (fast reads everywhere) | REGIONAL (home region fast) | GLOBAL for read-mostly catalog/tax; **never** for `inventory` counters |
| **Survival goal** | ZONE (lower write latency) | REGION (survives region loss, >= 3 regions) | ZONE by default; REGION only when a region outage must be survived |
| **Scaling reads** | Leaseholder (fresh) | Follower reads (stale, scalable) | Follower reads for history & display; **never** the oversell decrement |
| **Hot-SKU flash sale** | Single counter row | Sharded counter + FOR UPDATE + follower display reads | Shard + guard at scale; load-test at \~2x peak |
| **DR** | Multi-region survival (in-cluster HA) | Backups/PITR (logical recovery) | Both: they protect against different failures |
