This feature is in and is only available to enrolled organizations. To enroll your organization, contact your Cockroach Labs account team. This feature is subject to change.
Set up PCR on CockroachDB Advanced
In this guide, you’ll use the to set up PCR from a primary cluster to a standby cluster, monitor the PCR stream, and fail over from the primary to the standby cluster.PCR is supported on CockroachDB Advanced and CockroachDB self-hosted clusters. For a guide to setting up PCR on CockroachDB self-hosted, refer to the tutorial.
Before you begin
You’ll need the following:- Two CockroachDB Advanced clusters with the
supports_cluster_virtualizationflag enabled. For more information about cluster virtualization, read the . To set up PCR successfully, configure your clusters as per the following:- Clusters must be in the same cloud (AWS, GCP, or Azure).
- Clusters must be single (multiple availability zones per cluster is supported).
- The primary and standby cluster in AWS and Azure must be in different regions.
- The primary and standby cluster in GCP can be in the same region, but must not have overlapping CIDR ranges.
- Clusters can have different and . To avoid performance constraints (failover and redirecting application traffic to a standby), we recommend configuring the primary and standby clusters with similar hardware.
-
. To set up and manage PCR on CockroachDB Advanced clusters, you’ll use the
'https://cockroachlabs.cloud/api/v1/physical-replication-streams'endpoint. Access to thephysical-replication-streamsendpoint requires a valid CockroachDB Cloud with the correct permissions. The following describes the required roles for thephysical-replication-streamsendpoint methods. These can be assigned at the , , or cluster scope:
| Method | Required roles | Description |
|---|---|---|
POST | Create a PCR stream. Required on the primary and standby clusters. | |
GET | , , | Retrieve information for the PCR stream. Required on either the primary or standby cluster. |
PATCH | Update the PCR stream to fail over. Required on either the primary or standby cluster. |
Step 1. Create the clusters
To use PCR, it is necessary to set thesupports_cluster_virtualization field to true. This setting enables cluster virtualization, which is the architecture that supports PCR. For details on supported cluster cloud provider and region setup, refer to the prerequisites section.
If you are creating clusters in AWS or Azure, you must start the primary and standby clusters in different regions.
-
Send a
POSTto create the primary cluster:
- with your API secret key.
- with the cluster ID returned after creating the primary cluster.
-
Send a
POSTto create the standby cluster that includes your necessary cluster specification. Ensure that you includesupports_cluster_virtualizationset totrue:
- with your API secret key.
- with the cluster ID returned after creating the standby cluster.
Step 2. Start the PCR stream
We recommend using an empty standby cluster when starting PCR. When you initiate the PCR stream, CockroachDB Cloud will take a full cluster backup of the standby cluster, delete all data from the standby, and then start the PCR stream, which ensures that the standby will be fully consistent with the primary during PCR.
- Send a
POSTto the/v1/physical-replication-streamsendpoint to start the PCR stream:
- with your API secret key.
- with the cluster ID returned after creating the primary cluster.
- with the cluster ID returned after creating the standby cluster.
https://cockroachlabs.cloud/cluster/{your_cluster_id}/overview. The ID will resemble ad1e8630-729a-40f3-87e4-9f72eb3347a0.
Once you have started PCR, the standby cluster cannot accept writes and reads, therefore the and SQL shell will be unavailable prior to failover.
"id": The PCR stream’s ."status": The status of the PCR stream. For descriptions, refer to Status."primary_cluster_id","standby_cluster_id": The cluster IDs of the primary and standby clusters."created_at": The at which the PCR stream was created.
STARTING.
Step 3. Monitor the PCR stream
For monitoring the current status of the PCR stream, send aGET to the /v1/physical-replication-streams endpoint along with the ID of the PCR stream:
- with your API secret key.
- with the PCR job’s ID. You can find this in the response from when you created the PCR stream.
"id": The ID of the PCR stream."status": The status of the PCR stream. For descriptions, refer to Status."primary_cluster_id","standby_cluster_id": The cluster IDs of the primary and standby clusters."created_at": The time at which the PCR stream was created."retained_time": The timestamp indicating the lower bound that the PCR stream can failover to. The tracked replicated time and the advancing allows PCR to also track retained time."replicated_time": The latest time at which the standby cluster has consistent data. This field will be present when the PCR stream is in theREPLICATINGstate."replication_lag_seconds": The replication lag in seconds. This field will be present when the PCR stream is in theREPLICATINGstate.
Status
| Status | Description |
|---|---|
STARTING | Setting up between clusters and validating the connectivity. |
REPLICATING | Completing an initial scan and then continuing ongoing replication between the primary and standby clusters. |
FAILING\_OVER | Initiating the failover from the primary to the standby cluster. |
COMPLETED | The failover is complete and the standby cluster is now independent from the primary cluster. |
Metrics
For continual monitoring of PCR, track the following metrics with :physical_replication.logical_bytes: The logical bytes (the sum of all keys and values) ingested by all PCR streams.physical_replication.sst_bytes: The SST bytes (compressed) sent to the by all PCR streams.physical_replication.replicated_time_seconds: The replicated time of the PCR stream in seconds since the Unix epoch.
Fail over to the standby cluster
Failing over from the primary cluster to the standby cluster will stop the PCR stream, reset the standby cluster to a point in time where all ingested data is consistent, and mark the standby as ready to accept application traffic. You can schedule the failover to:- The latest consistent time.
- A time in the past within the
retained_time. - A time up to 1 hour in the future.
Fail over to the latest consistent time
To fail over to the latest consistent time, you only need to include"status": "FAILING_OVER" in your PATCH with the PCR stream ID:
Fail over to a specific time
To specify a timestamp, send aPATCH to the /v1/physical-replication-streams endpoint along with the primary cluster, standby cluster, or the ID of the PCR stream. Include the failover_at field with your required timestamp:
failover_at: The requested timestamp for failover. You can use"status":"FAILING_OVER"to initiate the failover and omitfailover_at, the failover time will default to the latest consistent replicated time.
GET to check when the failover is complete:
activated_at: The CockroachDB system time at which failover is finalized, which could be different from the time that failover was requested. This field will return a response when the PCR stream is inCOMPLETEDstatus.
PCR replicates on the cluster level, which means that the also replicates all system tables. Users that need to access the standby cluster after failover should use the user roles for the primary cluster, because the standby cluster is a copy of the primary cluster. PCR overwrites all previous system tables on the standby cluster.
Fail back to the primary cluster
To fail back from the standby to the primary cluster, start another PCR stream with the standby cluster as theprimary_cluster_id and the original primary cluster as the standby_cluster_id.
Technical reference
The replication happens at the byte level, which means that the job is unaware of databases, tables, row boundaries, and so on. However, when a failover to the standby cluster is initiated, the PCR job ensures that the cluster is in a transactionally consistent state as of a certain point in time. Beyond the application data, the job will also replicate users, privileges, and schema changes. At startup, the PCR job will set up VPC peering between the primary and standby Advanced clusters and validate the connectivity. During the job, are periodically emitting resolved timestamps, which is the time where the ingested data is known to be consistent. Resolved timestamps provide a guarantee that there are no new writes from before that timestamp. This allows the to move forward as the replicated timestamp advances, which permits the to continue as the PCR stream on the standby cluster advances.If the primary cluster does not receive replicated time information from the standby after 24 hours, it cancels the replication job. This ensures that an inactive replication job will not prevent garbage collection.

