Skip to main content
As you scale your usage of , you may need to keep certain subsets of data in specific localities. Keeping specific data on servers in specific geographic locations is also known as data domiciling. CockroachDB has basic support for data domiciling in multi-region clusters using the 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 . At a high level, this process involves:
  1. Controlling the placement of specific row or table data using regional tables with the and clauses.
  2. 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 .
When the cluster starts, you’ll see a message like the one shown below, followed by a SQL prompt. Note the URLs 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.
You now have a cluster running across 9 nodes, with 3 nodes each in the following regions:
  • us-east1
  • us-west1
  • europe-west1
You can verify this using the statement:

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.
Because the data in promo_codes is not updated frequently (a.k.a., “read-mostly”), and needs to be available from any region, the right table locality is .
Next, alter the 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.
All of the tables except 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. For more information about the performance considerations of rebalancing data in multi-region clusters, see . With the default settings, you should expect some replicas in the cluster to be violating this constraint. Those replicas will appear in the violatingConstraints 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:
It should return output like the following:
Using the output above, we can craft a curl invocation to call the critical nodes status endpoint:
Based on this output, you can see that several replicas are out of compliance for the reason described above: the presence of non-voting replicas in other regions to enable fast stale reads from those regions. To get more information about the ranges that are out of compliance, you can use a SQL statement like the one below.

Step 4. Apply stricter replica placement settings

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 the violatingConstraints key in the JSON response shows that there are no longer any replicas violating their constraints:
The output above shows that there are no replicas that do not meet the data domiciling goal. As described above, does not affect the replica placement for , 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 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_constraint option, inserting rows with DEFAULT for 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 --locality flag; 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_region enabled, 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 a Query has no home region error.

See also