Physical cluster replication is supported in CockroachDB self-hosted clusters and is in on .
- The system virtual cluster for administration tasks in both clusters, and starting the replication stream from the standby cluster.
- The application virtual cluster on the primary cluster to work with databases, tables, workloads, and so on.
Overview
The high-level steps in this tutorial are:- Create and start the primary cluster.
- Configure and create a user on the primary cluster.
- Create and start the standby cluster.
- Configure and create a user on the standby cluster.
- Securely copy certificates.
- Start the replication stream from the standby cluster.
To set up PCR from an existing CockroachDB cluster, which will serve as the primary cluster, refer to Set up PCR from an existing cluster.
Before you begin
- You need two separate CockroachDB clusters (primary and standby), each with a minimum of three nodes. The standby cluster should be the same version or one version ahead of the primary cluster. The primary and standby clusters must be configured with similar hardware profiles, number of nodes, and overall size. Significant discrepancies in the cluster configurations may result in degraded performance.
- To set up each cluster, you can follow . When you initialize the cluster with the command, you must pass the
--virtualizedor--virtualized-emptyflag. Refer to the cluster creation steps for the primary cluster and for the standby cluster for details. - The tutorial creates a self-signed certificate for each self-hosted cluster. To create certificates signed by an external certificate authority, refer to .
- To set up each cluster, you can follow . When you initialize the cluster with the command, you must pass the
- All nodes in each cluster will need access to the Certificate Authority for the other cluster. Refer to Manage cluster certificates.
- The primary and standby clusters can have different . However, behavior for features that rely on multi-region primitives, such as Region by Row and Region by Table, may be affected. For more information, refer to .
Step 1. Create the primary cluster
Initialize the primary cluster
To enable PCR, it is necessary to initialize the CockroachDB cluster with the appropriate flag to create the appropriate virtual clusters on the primary and standby cluster. When initializing the primary cluster, you pass the--virtualized flag to create a with a system virtual cluster and a main virtual cluster. When initializing the standby cluster, you pass the --virtualized-empty flag to create a virtualized standby cluster that contains a system virtual cluster.
For example, the cockroach init command to initialize the primary cluster (according to the ):
Connect to the primary cluster system virtual cluster
Connect to your primary cluster’s system virtual cluster using .-
To connect to the system virtual cluster, pass the
options=-ccluster=systemparameter in the URL:The prompt will includesystemwhen you are connected to the system virtual cluster.
You should only connect to the system virtual cluster for cluster administration. To work with databases, tables, or workloads, connect to a virtual cluster.
-
Set the
kv.rangefeed.enabledcluster setting totrue. The replication job connects to a long-lived request, a rangefeed, which pushes changes as they happen: -
Confirm the status of your virtual cluster:
The output will include the
systemvirtual cluster and themainvirtual cluster:Because this is the primary cluster rather than the standby cluster, thedata_stateof all rows isready, rather thanreplicatingor another .
Create a user with replication privileges
The standby cluster connects to the primary cluster’s system virtual cluster using an identity with theREPLICATIONSOURCE . Connect to the primary cluster’s system virtual cluster and create a user with a password:
-
From the primary’s system virtual cluster SQL shell, create a user and password:
If you need to change the password later, refer to .
-
Grant the to your user:
Connect to the primary virtual cluster (optional)
-
If you would like to run a sample workload on the primary’s virtual cluster, open a new terminal window and use to run the workload.
For example, to initiate the workload:
Replace and with a node’s IP address or hostname and port. The
cockroach workloadcommand does not support connection or security flags like other . Instead, you must use a at the end of the command. As a result, for the example in this tutorial, you will need:options=-ccluster=mainsslmode=verify-fullsslrootcert={path}/certs/ca.crt: the path to the CA certificate.sslcert={path}/certs/client.root.crt: the path to the client certificate.sslkey={path}/certs/client.root.key: the path to the client private key.
-
Run the
movrworkload for a set duration using the same connection string: -
To connect to the primary cluster’s virtual cluster, use the
options=-ccluster={virtual_cluster_name}parameter:The prompt will includemainwhen you are connected to the virtual cluster. -
Create a user for your primary cluster’s
mainvirtual cluster: -
You can connect to the with this user to observe activity on the primary cluster. Open a web browser at
https://{node IP or hostname}:8080/and enter your credentials.
Step 2. Create the standby cluster
Initialize the standby cluster
Similarly to the primary cluster, you must initialize the standby cluster with the--virtualized-empty flag. This creates a virtualized cluster with a system virtual cluster.
For example, the cockroach init command to initialize the standby cluster (according to the ):
Connect to the standby cluster system virtual cluster
Connect to your standby cluster’s system virtual cluster using .-
To connect to the system virtual cluster, pass the
options=-ccluster=systemparameter in the URL: -
Set the
kv.rangefeed.enabledcluster setting totrue. The replication job connects to a long-lived request, a rangefeed, which pushes changes as they happen: -
Confirm the status of your virtual cluster:
The output will show the
systemvirtual cluster, but nomainvirtual cluster:
Create a user with replication privileges on the standby cluster
Create a user to run the PCR stream and access the to observe the job:-
Create a user:
-
To observe the replication activity, your user will need privileges:
Open the DB Console in your web browser:
https://{node IP or hostname}:8080/, where you will be prompted for these credentials. Refer to for more detail on tracking relevant metrics for your replication stream.
Step 3. Manage cluster certificates and generate connection strings
It is important to carefully manage the exchange of CA certificates between clusters if you have generated self-signed certificates with
cockroach cert as part of the .To create certificates signed by an external certificate authority, refer to .cockroach convert-url command to generate a connection string containing a cluster’s certificate for any that require a connection string. Pass the connection URL for the replication user, along with the path to the certificate for the primary cluster, into the convert-url command:
Step 4. Start replication
The system virtual cluster in the standby cluster initializes and controls the replication stream by pulling from the primary cluster. In this section, you will connect to the primary from the standby to initiate the replication stream.-
In the SQL shell on the standby cluster, create an external connection using the primary cluster’s connection string. Prefix the
postgresql://scheme to the connection string and replace with your external connection name:If the primary and standby cluster nodes are on different networks, you can route the replication stream through the primary cluster’s load balancer. Add&crdb_route=gatewayto the connection string:For an optimally performant replication stream, all nodes on the primary and standby clusters should share the same virtual network. The gateway route option should only be used when this network configuration is not possible due to firewall or IP allocation constraints. Once the external connection has been created, create the replication stream from the standby cluster:Once the standby cluster has made a connection to the primary cluster, the standby pulls the topology of the primary cluster and distributes the replication work across all nodes in the primary and standby.
-
To view all virtual clusters on the standby, run:
The standby cluster will show the
mainvirtual cluster is in areplicatingstate.The standby cluster’s virtual cluster is offline while the replication stream is running. To bring it online, you must explicitly . -
To manage the replication stream, you can the replication stream as well as the current details for the job:
With the replication stream running, you can monitor the job via the DB Console, SQL shell, or Prometheus. You can also verify data is correct on the standby cluster at a specific point in time. For more detail, refer to .
You cannot pause a PCR job for longer than 24 hours. PCR jobs paused for longer than 24 hours fail and cannot be recovered.
Set up PCR from an existing cluster
You can set up PCR replication from an existing CockroachDB cluster that does not have enabled. However, the standby cluster must have cluster virtualization enabled. In the , the existing cluster is the primary cluster.When you start PCR with an existing primary cluster that does not have enabled, you will not be able to to the original primary cluster from the promoted, original standby.For more details on the failback process when you have started PCR with a non-virtualized primary, refer to .
- An existing primary cluster. If you need to upgrade your existing cluster, refer to .
- A standby cluster that is at the same version or one version ahead of the primary cluster. To set up the cluster, you can follow . When you initialize the cluster with the command, you must pass the
--virtualized-emptyflag. For details, refer to the cluster creation steps for the standby cluster. - Review the remaining prerequisites at the start of this page, which also apply to running PCR from an existing cluster.
Example
- To configure the standby cluster ready for PCR, follow the steps outlined in Connect to the standby cluster system virtual cluster that include enabling the necessary cluster settings.
-
Create a user on both clusters and grant the
SYSTEM REPLICATIONprivilege to the created user on each cluster: -
View the virtual clusters on both clusters:
You will find that both clusters only have the
systemvirtual cluster: -
To create the replication job, you will need a connection string for the primary cluster containing its CA certificate. For steps to generate a connection string with
cockroach convert-url, refer to Step 3. Manage cluster certificates and generate connection strings. -
If you would like to run a test workload on your existing primary cluster, you can use like the following:
-
To start the replication, you will need to replicate
systemfrom the existing primary cluster in order to create a new virtual cluster on the standby cluster (mainin this example). On the standby cluster, run:This statement includes:main: The name of the virtual cluster to create on the standby cluster.system: The system virtual cluster on the existing primary cluster.- The connection string for the existing primary cluster.
-
View the virtual clusters on the standby cluster:
The output shows the newly created
mainvirtual cluster and that the replication job is :
Connection reference
This table outlines the connection strings you will need for this setup tutorial. For additional detail on the standard CockroachDB connection parameters, refer to . The table usesmain as an example name for the virtual cluster that contains user table data in the primary and standby clusters.
| Cluster | Virtual Cluster | Usage | URL and Parameters |
|---|---|---|---|
| Primary | System | Set up a replication user and view running virtual clusters. Connect with . | "postgresql://root@{node IP or hostname}:{26257}?options=-ccluster=system&sslmode=verify-full"
--certs-dir flag to specify the path to your certificate. |
| Primary | Main | Add and run a workload with . | "postgresql://root@{node IP or hostname}:{26257}?options=-ccluster=main&sslmode=verify-full&sslrootcert=certs/ca.crt&sslcert=certs/client.root.crt&sslkey=certs/client.root.key"The cockroach workload command does not support connection or security flags like other . Instead, you must use a at the end of the command. As a result, for the example in this tutorial, you will need:
|
| Standby | System | Manage the replication stream. Connect with . | "postgresql://root@{node IP or hostname}:{26257}?options=-ccluster=system&sslmode=verify-full"
--certs-dir flag to specify the path to your certificate. |
| Standby/Primary | System | Connect to the other cluster. | "postgresql://{replication user}:{password}@{node IP or hostname}:{26257}/defaultdb?options=-ccluster%3Dsystem&sslinline=true&sslmode=verify-full&sslrootcert=-----BEGIN+CERTIFICATE-----{encoded_cert}-----END+CERTIFICATE-----%0A"Generate the connection string with cockroach convert-url. Use the generated connection string in:
|
| Standby/Primary | System | Connect to the other cluster through its load balancer | "postgresql://{replication user}:{password}@{node IP or hostname}:{26257}/defaultdb?options=-ccluster%3Dsystem&crdb_route=gateway&sslinline=true&sslmode=verify-full&sslrootcert=-----BEGIN+CERTIFICATE-----{encoded_cert}-----END+CERTIFICATE-----%0A"Generate the connection string with cockroach convert-url and manually add the &crdb_route=gateway parameter. Recommended only when nodes do not share the same network. Use the generated connection string in:
|
| Standby | Read only | Run read queries on the standby’s replicating virtual cluster | "postgresql://root@{node IP or hostname}:{26257}?options=-ccluster=main-readonly&sslmode=verify-full"
--certs-dir flag to specify the path to your certificate. |

