ALTER DATABASE ... ADD SUPER REGION statement.
Using CockroachDB as part of your approach to data domiciling has several limitations. For more information, see Known limitations.
Overview
This page has instructions for data domiciling in multi-region clusters. At a high level, this process involves:- Controlling the placement of specific row or table data using regional tables with the
REGIONAL BY ROWandREGIONAL BY TABLEclauses. - Further restricting where the data in those regional tables is stored using the
ALTER DATABASE ... ADD SUPER REGION, which creates a set of database regions such that regional tables and regional by row tables whose home regions are in the super region will have all of their replicas stored only in regions that are members of the super region. For more information, see Super regions.
An alternative method to the
ALTER DATABASE ... ADD SUPER REGION statement is to use the ALTER DATABASE ... PLACEMENT RESTRICTED statement.PLACEMENT RESTRICTED is not recommended, and is documented for backwards compatibility. Most users should use ADD SUPER REGION, which allows for region survival as well as providing data placement.Before you begin
This page assumes you are already familiar with:- CockroachDB’s multi-region SQL abstractions. If you are not using them, the instructions on this page will not apply.
- The fact that CockroachDB stores your data in a distributed key-value store, which is split into chunks called ranges.
Example
In the following example, you will go through the process of configuring the MovR data set using multi-region SQL statements. Then, as part of implementing a data domiciling strategy, you will apply restricted replica settings in Step 4 using theALTER DATABASE ... ADD SUPER REGION statement. Super regions work with zone survival goals and region survival goals.
Finally, you will verify that the resulting replica placements are as expected using the critical nodes status endpoint.
For the purposes of this example, the data domiciling requirement is to configure a multi-region deployment of the MovR database such that data for EU-based users, vehicles, etc. is being stored on CockroachDB nodes running in EU localities.
Step 1. Start a simulated multi-region cluster
Use the followingcockroach demo command to start the cluster. This particular combination of flags results in a demo cluster of 9 nodes, with 3 nodes in each region. It sets the appropriate node localities and also simulates the network latency that would occur between nodes in these localities. For more information about each flag, see the cockroach demo documentation, especially for --global.
- Viewing the DB Console:
http://127.0.0.1:8080/demologin?password=demo30570&username=demo. - Connecting to the database from a SQL shell or a programming language:
postgresql://demo:demo30570@127.0.0.1:26257/movr?sslmode=require&sslrootcert=%2FUsers%2Frloveland%2F.cockroach-demo%2Fca.crt.
us-east1us-west1europe-west1
SHOW REGIONS statement:
Step 2. Apply multi-region SQL abstractions
Execute the following statements to set the database regions. This information is necessary so that CockroachDB can later move data around to optimize access to particular data from particular regions.promo_codes is not updated frequently (a.k.a., “read-mostly”), and needs to be available from any region, the right table locality is GLOBAL.
user_promo_codes table to have a foreign key into the global promo_codes table. This will enable fast reads of the promo_codes.code column from any region in the cluster.
promo_codes contain rows which are partitioned by region, and updated very frequently. For these tables, the right table locality for optimizing access to their data is REGIONAL BY ROW.
Apply this table locality to the remaining tables. These statements use a CASE statement to put data for a given city in the right region and can take around 1 minute to complete for each table.
-
rides -
user_promo_codes -
users -
vehicle_location_histories -
vehicles
Step 3. View noncompliant replicas
Next, check the critical nodes status endpoint to see which ranges are still not in compliance with your desired domiciling: that data on EU-based entities (users, etc.) does not leave EU-based nodes. On a small demo cluster like this one, the data movement from the previous step should finish quickly; on larger clusters, the rebalancing process may take longer. With the default settings, you should expect some replicas in the cluster to be violating this constraint. Those replicas will appear in theviolatingConstraints field of the output.
This is because non-voting replicas are enabled by default in multi-region clusters to enable stale reads of data in regional tables from outside those tables’ home regions. For many use cases, this is preferred, but it keeps you from meeting the domiciling requirements for this example.
In order to check the critical nodes status endpoint you first need to get an authentication cookie. To get an authentication cookie, run the cockroach auth-session login command:
curl invocation to call the critical nodes status endpoint:
SHOW RANGES SQL statement like the one below.
Step 4. Apply stricter replica placement settings
- (Recommended) Use ADD SUPER REGION
- (Not recommended) Use PLACEMENT RESTRICTED
To ensure that data on EU-based users, vehicles, etc. from You have now created a super region with only one region. The updated replica placement settings should start to apply immediately.This single-region super region constrains replica placement, but it cannot remain available through the loss of that region. To survive the loss of one region, a super region must contain at least 3 regions and use an explicit or inherited
REGIONAL BY ROW tables is stored only on EU-based nodes in the cluster, you can use Super regions. This will ensure that regional tables and regional by row tables whose home regions are in the super region will have all of their replicas stored only in regions that are members of the super region.Use the ALTER DATABASE ... ADD SUPER REGION statement:REGION survival goal.ALTER DATABASE ... ADD SUPER REGION does not affect the replica placement for global tables, which are designed to provide fast, up-to-date reads from all database regions.Step 5. Verify updated replica placement
Now that you have restricted the placement of non-voting replicas for all regional tables, you can check the critical nodes status endpoint to see the effects. In a few seconds, you should see that theviolatingConstraints key in the JSON response shows that there are no longer any replicas violating their constraints:
ALTER DATABASE ... PLACEMENT RESTRICTED does not affect the replica placement for GLOBAL tables, so these replicas are considered to be in compliance.
Now that you have verified that the system is configured to meet the domiciling requirement, it’s a good idea to check the critical nodes status endpoint on a regular basis (via automation of some kind) to ensure that the requirement continues to be met.
The steps above are necessary but not sufficient to accomplish a data domiciling solution using CockroachDB. Be sure to review the limitations of CockroachDB for data domiciling and design your total solution with those limitations in mind.
Known limitations
Using CockroachDB as part of your approach to data domiciling has several limitations:- When using the
infer_rbr_region_col_using_constraintoption, inserting rows withDEFAULTfor the region column uses the database’s primary region instead of inferring the region from the parent table via foreign-key constraint. - When columns are indexed, a subset of data from the indexed columns may appear in meta ranges or other system tables. CockroachDB synchronizes these system ranges and system tables across nodes. This synchronization does not respect any multi-region settings applied via either the multi-region SQL statements, or the low-level zone configs mechanism.
- Zone configs can be used for data placement but these features were historically built for performance, not for domiciling. The replication system’s top priority is to prevent the loss of data and it may override the zone configurations if necessary to ensure data durability. For more information, see Replication Controls.
- A super region’s survival goal does not change replica placement for the
systemdatabase. If ranges in thesystemdatabase lose quorum, cluster availability can be affected even when data in a super region remains available. - If your log files are kept in the region where they were generated, there is some cross-region leakage (like the system tables described previously), but the majority of user data that makes it into the logs is going to be homed in that region. If that’s not strong enough, you can use the log redaction functionality to strip all raw data from the logs. You can also limit your log retention entirely.
- If you start a node with a
--localityflag that says the node is in region A, but the node is actually running in some region B, data domiciling based on the inferred node placement will not work. A CockroachDB node only knows its locality based on the text supplied to the--localityflag; it can not ensure that it is actually running in that physical location. - Secondary regions are not compatible with databases containing
REGIONAL BY ROWtables. CockroachDB does not prevent you from defining secondary regions on databases with regional by row tables, but the interaction of these features is not supported. Therefore, Cockroach Labs recommends that you avoid defining secondary regions on databases that use regional by row table configurations. - With
enforce_home_regionenabled, CockroachDB currently validates home-region access during plan build. This can falsely reject queries (e.g., lookup joins) that would only read local data at execution time, returning aQuery has no home regionerror.
See also
- How to Choose a Multi-region Configuration
- Multi-Region Overview
- Multi-Region Capabilities Overview
- Reads and Writes in CockroachDB
- When to Use
REGIONALvs.GLOBALTables - When to Use
ZONEvs.REGIONSurvival Goals ADD REGION- Secondary regions
- Zone Config Extensions
ALTER DATABASE ... SET SECONDARY REGIONALTER DATABASE ... DROP SECONDARY REGION

