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 . At a high level, this process involves:- Controlling the placement of specific row or table data using regional tables with the and clauses.
- Further restricting where the data in those regional tables is stored using the , which creates a set of such that and whose are in the super region will have all of their stored only in regions that are members of the super region. For more information, see .
An alternative method to the statement is to use the 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 . If you are not using them, the instructions on this page will not apply.
- The fact that CockroachDB stores your data in .
Example
In the following example, you will go through the process of configuring the data set using . Then, as part of implementing a data domiciling strategy, you will apply restricted replica settings in Step 4 using the : It works with databases with or . If you need region survival goals, you must use . Finally, you will verify that the resulting replica placements are as expected using the . For the purposes of this example, the data domiciling requirement is to configure a multi-region deployment of the 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 following 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 and also simulates the network latency that would occur between nodes in these localities. For more information about each flag, see the documentation, especially for .- Viewing the :
http://127.0.0.1:8080/demologin?password=demo30570&username=demo. - Connecting to the database from a or a :
postgresql://demo:demo30570@127.0.0.1:26257/movr?sslmode=require&sslrootcert=%2FUsers%2Frloveland%2F.cockroach-demo%2Fca.crt.
us-east1us-west1europe-west1
Step 2. Apply multi-region SQL abstractions
Execute the following statements to set the . 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 .
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 .
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 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 are enabled by default in to enable stale reads of data in from outside those tables’ . 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 command:
curl invocation to call the critical nodes status endpoint:
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 is stored only on EU-based nodes in the cluster, you can use . This will ensure that and whose are in the super region will have all of their stored only in regions that are members of the super region.To use this statement, you must set the Next, use the statement:You have now created a super region with only one region. The updated replica placement settings should start to apply immediately.
enable_super_regions : does not affect the replica placement for , which are designed to provide fast, up-to-date reads from all .
Step 5. Verify updated replica placement
Now that you have restricted the placement of non-voting replicas for all , you can check the 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:
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 , a subset of data from the indexed columns may appear in 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 , or the low-level mechanism.
- 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 .
- If your 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 to strip all raw data from the logs. You can also limit your log retention entirely.
- If you start a node with a flag 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. - are not compatible with databases containing tables. 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.

