> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Physical Cluster Replication

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

CockroachDB **physical cluster replication (PCR)** continuously sends all data at the cluster level from a *primary* cluster to an independent *standby* cluster. Existing data and ongoing changes on the active primary cluster, which is serving application data, replicate asynchronously to the passive standby cluster.

You can <InternalLink path="failover-replication">*fail over*</InternalLink> from the primary cluster to the standby cluster. This will stop the replication stream, reset the standby cluster to a point in time (in the past or future) where all ingested data is consistent, and make the standby ready to accept application traffic.

For a list of requirements for PCR, refer to the <InternalLink path="set-up-physical-cluster-replication#before-you-begin">Before you begin</InternalLink> section of the <InternalLink path="set-up-physical-cluster-replication">setup tutorial</InternalLink>.

<Tip>
  Cockroach Labs also has a <InternalLink path="logical-data-replication-overview">logical data replication</InternalLink> tool that continuously replicates tables between an active *source* CockroachDB cluster to an active *destination* CockroachDB cluster. Both source and destination can receive application reads and writes, and participate in <InternalLink path="logical-data-replication-overview#use-cases">*bidirectional*</InternalLink> LDR for eventual consistency in the replicating tables.
</Tip>

## Use cases

You can use PCR to:

* Meet your RTO (Recovery Time Objective) and RPO (Recovery Point Objective) requirements. PCR provides lower RTO and RPO than <InternalLink path="backup-and-restore-overview">backup and restore</InternalLink>.
* Automatically replicate everything in your primary cluster to recover quickly from a control plane or full cluster failure.
* Protect against region failure when you cannot use individual <InternalLink path="multiregion-overview">multi-region clusters</InternalLink>—for example, if you have a two-datacenter architecture and do not have access to three regions; or, you need low-write latency in a single region. PCR allows for an active-passive (primary-standby) structure across two clusters with the passive cluster in a different region.
* Quickly recover from user error (for example, dropping a database) by <InternalLink path="failover-replication">failing over</InternalLink> to a time in the near past.
* Create a [blue-green deployment model](https://en.wikipedia.org/wiki/Blue%E2%80%93green_deployment) by using the standby cluster for testing upgrades and hardware changes.

## Features

* **Asynchronous cluster-level replication**: When you initiate a replication stream, it will replicate byte-for-byte all of the primary cluster's existing user data and associated metadata to the standby cluster asynchronously. From then on, it will continuously replicate the primary cluster's data and metadata to the standby cluster. PCR will automatically replicate changes related to operations such as <InternalLink path="online-schema-changes">schema changes</InternalLink>, user and <InternalLink path="security-reference/authorization#managing-privileges">privilege</InternalLink> modifications, and <InternalLink path="show-zone-configurations">zone configuration</InternalLink> updates without any manual work.
* **Transactional consistency**: Avoid conflicts in data after recovery; the replication completes to a transactionally consistent state.
* **Improved RPO and RTO**: Depending on workload and deployment configuration, <InternalLink path="physical-cluster-replication-technical-overview">replication lag</InternalLink> between the primary and standby is generally in the tens-of-seconds range. The failover process from the primary cluster to the standby should typically happen within five minutes when completing a failover to the latest replicated time using <InternalLink path="alter-virtual-cluster#synopsis">`LATEST`</InternalLink>.
* **Failover to a timestamp in the past or the future**: In the case of logical disasters or mistakes, you can <InternalLink path="failover-replication">fail over</InternalLink> from the primary to the standby cluster to a timestamp in the past. This means that you can return the standby to a timestamp before the mistake was replicated to the standby. Furthermore, you can plan a failover by specifying a timestamp in the future.
* **Fast failback**: Switch back from the promoted standby cluster to the original primary cluster after a failover event without an initial scan.
* **Read from standby cluster**: You can configure PCR to allow `SELECT` queries on the standby cluster. For more details, refer to <InternalLink path="create-virtual-cluster#start-a-pcr-stream-with-read-from-standby">Start a PCR stream with read from standby</InternalLink>.
* **Monitoring**: To monitor the replication's initial progress, current status, and performance, you can use metrics available in the <InternalLink path="ui-overview">DB Console</InternalLink> and <InternalLink path="monitor-cockroachdb-with-prometheus">Prometheus</InternalLink>. For more details, refer to <InternalLink path="physical-cluster-replication-monitoring">Physical Cluster Replication Monitoring</InternalLink>.

## Known limitations

* Physical cluster replication is supported in CockroachDB self-hosted clusters on v23.2 or later. The primary cluster can be a <InternalLink path="set-up-physical-cluster-replication#step-1-create-the-primary-cluster">new</InternalLink> or <InternalLink path="set-up-physical-cluster-replication#set-up-pcr-from-an-existing-cluster">existing</InternalLink> cluster. The standby cluster must be a <InternalLink path="set-up-physical-cluster-replication#step-2-create-the-standby-cluster">new cluster started with the `--virtualized-empty` flag</InternalLink>.
* The primary and standby clusters must have the same <InternalLink path="configure-replication-zones">zone configurations</InternalLink>.
* Before failover to the standby, the standby cluster does not support running <InternalLink path="backup-and-restore-overview">backups</InternalLink> or <InternalLink path="change-data-capture-overview">changefeeds</InternalLink>.
* After a failover, there is no mechanism to stop applications from connecting to the original primary cluster. It is necessary to redirect application traffic manually, such as by using a network load balancer or adjusting DNS records.

<Note>
  Frequent large schema changes or imports may cause a significant spike in <InternalLink path="physical-cluster-replication-technical-overview">replication lag</InternalLink>.
</Note>

## Get started

This section is a quick overview of the initial requirements to start a replication stream. For more comprehensive guides, refer to:

* <InternalLink path="set-up-physical-cluster-replication">Set Up Physical Cluster Replication</InternalLink>: for a tutorial on how to start a replication stream.
* <InternalLink path="physical-cluster-replication-monitoring">Physical Cluster Replication Monitoring</InternalLink>: for detail on metrics and observability into a replication stream.
* <InternalLink path="failover-replication">Fail Over from a Primary Cluster to a Standby Cluster</InternalLink>: for a guide on how to complete a replication stream and fail over to the standby cluster.
* <InternalLink path="physical-cluster-replication-technical-overview">Technical Overview</InternalLink>: to understand PCR in more depth before setup.

### Manage replication in the SQL shell

To start, manage, and observe PCR, you can use the following SQL statements:

| Statement                                                                                                       | Action                                                                        |
| --------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| <InternalLink path="create-virtual-cluster">`CREATE VIRTUAL CLUSTER ... FROM REPLICATION OF ...`</InternalLink> | Start a replication stream.                                                   |
| <InternalLink path="alter-virtual-cluster">`ALTER VIRTUAL CLUSTER ... PAUSE REPLICATION`</InternalLink>         | Pause a running replication stream.                                           |
| <InternalLink path="alter-virtual-cluster">`ALTER VIRTUAL CLUSTER ... RESUME REPLICATION`</InternalLink>        | Resume a paused replication stream.                                           |
| <InternalLink path="alter-virtual-cluster">`ALTER VIRTUAL CLUSTER ... START SERVICE SHARED`</InternalLink>      | Initiate a <InternalLink path="failover-replication">failover</InternalLink>. |
| <InternalLink path="show-virtual-cluster">`SHOW VIRTUAL CLUSTER`</InternalLink>                                 | Show all virtual clusters.                                                    |
| <InternalLink path="drop-virtual-cluster">`DROP VIRTUAL CLUSTER`</InternalLink>                                 | Remove a virtual cluster.                                                     |

## Cluster versions and upgrades

<Danger>
  The standby cluster must be at the same version as, or one version ahead of, the primary's virtual cluster.
</Danger>

When PCR is enabled, upgrade with the following procedure. This upgrades the standby cluster before the primary cluster. Within the primary and standby CockroachDB clusters, the system virtual cluster must be at a cluster version greater than or equal to the virtual cluster:

1. <InternalLink path="upgrade-cockroach-version#perform-a-major-version-upgrade">Upgrade the binaries</InternalLink> on the primary and standby clusters. Replace the binary on each node of the cluster and restart the node.
2. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the standby's system virtual cluster if auto-finalization is disabled.
3. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the primary's system virtual cluster if auto-finalization is disabled.
4. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the standby's virtual cluster.
5. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the primary's virtual cluster.

The standby cluster must be at the same version as, or one version ahead of, the primary's virtual cluster at the time of <InternalLink path="failover-replication">failover</InternalLink>.

## Demo video

Learn how to use PCR to meet your RTO and RPO requirements with the following demo:

<iframe width="560" height="315" src="https://www.youtube.com/embed/VDqw4XIpEAk" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />
