Read from Standby

On this page Carat arrow pointing down

In addition to providing failover capabilities for disaster recovery, physical cluster replication (PCR) allows you to direct read-only queries to your standby cluster. This process offloads traffic such as application reads, analytics queries, and ad-hoc reporting from the primary cluster.

Use this page to understand how the read from standby feature works and how to utilize it.

How the read from standby feature works

PCR utilizes cluster virtualization to separate a cluster's control plane from its data plane. A cluster always has one control plane, called a system virtual cluster (system VC), and at least one data plane, called an App Virtual Cluster (app VC). The standby cluster's system VC manages the PCR job and other cluster metadata, and is not used for application queries. All data tables, system tables, and cluster settings in the standby cluster's app VC are identical to the primary cluster's app VC. The standby cluster's app VC itself remains offline during replication.

When using read from standby, applications can read from the standby cluster, but they do not connect directly to the standby cluster's app VC. Instead, PCR introduces a reader virtual cluster (reader VC). The reader VC ensures a clean, isolated environment specifically for serving read queries without interfering with replication or system metadata. It reads continuously from the standby cluster's app VC using internal pointers, providing access to the replicated data while keeping the app VC offline. The reader VC itself only stores a small amount of metadata and no user data, so it is not expected to take up additional storage space.

The standby cluster's reader VC has its own system tables and cluster settings. The reader VC replicates a subset of system tables, including Users and Roles, from the app VC, so that existing primary users can authenticate using the same users and roles as on the primary cluster's app VC. Other system tables and cluster settings are set to defaults in the reader VC. For more information, consult Physical Cluster Replication Technical Overview.

In the event of failover, the reader VC is destroyed.

Use the read from standby feature

Before you begin

Prior to setting up read from standby, ensure that:

  • You have already configured PCR between a primary cluster and a standby cluster. For information on configuring PCR, refer to Set Up Physical Cluster Replication.
  • Your CockroachDB version is v24.3 or later. The read from standby option is not supported in earlier versions.

Start a PCR stream with read from standby

To start a PCR stream that allows read access to the standby cluster, use the CREATE VIRTUAL CLUSTER ... REPLICATION statement with the READ VIRTUAL CLUSTER option:

icon/buttons/copy
CREATE VIRTUAL CLUSTER main FROM REPLICATION OF main ON 'postgresql://{connection string to primary}' WITH READ VIRTUAL CLUSTER;

Add read from standby to a PCR stream

To add read from standby capabilities to an existing PCR stream, use the ALTER VIRTUAL CLUSTER statement:

icon/buttons/copy
ALTER VIRTUAL CLUSTER main SET REPLICATION READ VIRTUAL CLUSTER;
Note:

The standby cluster's app VC must have a status of replicating before you can create your reader VC. Use the SHOW VIRTUAL CLUSTERS command to check the status of the app VC.

Check the status of your reader virtual cluster

To confirm that your reader virtual cluster is active:

icon/buttons/copy
SHOW VIRTUAL CLUSTERS;

The output shows a standby-readonly virtual cluster in addition to the system VC and app VC:

  id |       name       | data_state  | service_mode
-----+------------------+-------------+---------------
   1 | system           | ready       | shared
   3 | standby          | replicating | none
   4 | standby-readonly | ready       | shared
Note:

The reader VC cannot serve reads until after the PCR initial scan is complete. After completing the initial scan, wait until the reader VC's service_mode is shared, then wait about one minute before connecting to the reader VC.

Run read-only queries on the standby cluster

Once you have created a reader virtual cluster on the standby cluster, you can connect to it and run read (SELECT) queries. For example:

icon/buttons/copy
SELECT COUNT(*) FROM customers;
SELECT region, SUM(amount) FROM orders GROUP BY region;

The results of queries on the standby cluster reflect the state of the primary cluster as of a historical time that approaches the replicated time.

Note:

Write operations are not permitted on the standby cluster.

See also

×