> ## 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.

# Set Up Logical Data 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>;
};

export const version = "v24.3";

<Note>
  **This feature is in <InternalLink path="cockroachdb-feature-availability">preview</InternalLink>** and subject to change. To share feedback and/or issues, contact [Support](https://support.cockroachlabs.com).
</Note>

Logical data replication is only supported in CockroachDB self-hosted clusters.
In this tutorial, you will set up <InternalLink path="logical-data-replication-overview">**logical data replication (LDR)**</InternalLink> streaming data from a source table to a destination table between two CockroachDB clusters. Both clusters are active and can serve traffic. You can apply the outlined steps to create *unidirectional* LDR from a source table to a destination table (cluster A to cluster B) in one LDR job. Optionally, you can also create *bidirectional* LDR from cluster B's table to cluster A's table by starting a second LDR job. In a bidirectional setup, each cluster operates as both a source and a destination in separate LDR jobs.

For more details on use cases, refer to the <InternalLink path="logical-data-replication-overview">Logical Data Replication Overview</InternalLink>.

<img src="https://mintcdn.com/cockroachlabs/Ww46ooILgIVlX3C1/images/v24.3/bidirectional-stream.svg?fit=max&auto=format&n=Ww46ooILgIVlX3C1&q=85&s=c8f66fc9d52672cd1cd9abe13180fbf7" alt="Diagram showing bidirectional LDR from cluster A to B and back again from cluster B to A." width="512" height="261" data-path="images/v24.3/bidirectional-stream.svg" />

## Tutorial overview

If you're setting up bidirectional LDR, both clusters will act as a source and a destination in the respective LDR jobs. The high-level steps are:

1. Prepare the tables on each cluster with the prerequisites for starting LDR.
2. Set up an <InternalLink path="create-external-connection">external connection</InternalLink> on cluster B (which will be the destination cluster initially) to hold the connection URI for cluster A.
3. Start LDR from cluster B with your required options.
4. (Optional) Run Steps 1 to 3 again with cluster B as the source and A as the destination, which starts LDR streaming from cluster B to A.
5. Check the status of the LDR job in the <InternalLink path="ui-overview">DB Console</InternalLink>.

## Before you begin

You'll need:

* Two separate v24.3 CockroachDB self-hosted clusters with connectivity between every node in both clusters. That is, all nodes in cluster A must be able to contact each node in cluster B and vice versa. The SQL advertised address should be the cluster node advertise address so that the LDR job can plan node-to-node connections between clusters for maximum performance. The source and destination 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 <InternalLink path="deploy-cockroachdb-on-premises">Deploy CockroachDB on Premises</InternalLink>.
  * The <InternalLink path="deploy-cockroachdb-on-premises">Deploy CockroachDB on Premises</InternalLink> tutorial creates a self-signed certificate for each self-hosted cluster. To create certificates signed by an external certificate authority, refer to <InternalLink path="create-security-certificates-openssl">Create Security Certificates using OpenSSL</InternalLink>.
  * All nodes in each cluster will need access to the Certificate Authority for the other cluster. Refer to [Step 2. Connect from the destination to the source](#step-2-connect-from-the-destination-to-the-source).
* LDR replicates at the table level, which means clusters can contain other tables that are not part of the LDR job. If both clusters are empty, create the tables that you need to replicate with **identical** schema definitions (excluding indexes) on both clusters. If one cluster already has an existing table that you'll replicate, ensure the other cluster's table definition matches. For more details on the supported schemas, refer to [Schema Validation](#schema-validation).

To create bidirectional LDR, you can complete the [optional step](#step-4-optional-set-up-bidirectional-ldr) to start the second LDR job that sends writes from the table on cluster B to the table on cluster A.

### Schema validation

Before you start LDR, you must ensure that all column names, types, constraints, and unique indexes on the destination table match with the source table.

You cannot use LDR on a table with a schema that contains the following:

* <InternalLink path="column-families">Column families</InternalLink>
* <InternalLink path="partial-indexes">Partial indexes</InternalLink> and <InternalLink path="hash-sharded-indexes">hash-sharded indexes</InternalLink>
* Indexes with a <InternalLink path="computed-columns">virtual computed column</InternalLink>
* Composite types in the <InternalLink path="primary-key">primary key</InternalLink>
* <InternalLink path="foreign-key">Foreign key</InternalLink> dependencies

For more details, refer to the LDR <InternalLink path="logical-data-replication-overview#known-limitations">Known limitations</InternalLink>.

LDR does not support replicating a table with <InternalLink path="foreign-key">foreign key constraints</InternalLink>.

#### Unique secondary indexes

When the destination table includes unique <InternalLink path="schema-design-indexes">secondary indexes</InternalLink>, it can cause rows to enter the <InternalLink path="manage-logical-data-replication">*dead letter queue* (DLQ)</InternalLink>. The two clusters in LDR operate independently, so writes to one cluster can conflict with writes to the other.

If the application modifies the same row in both clusters, LDR resolves the conflict using <InternalLink path="manage-logical-data-replication#conflict-resolution">*last write wins* (LWW)</InternalLink> conflict resolution. <InternalLink path="unique">`UNIQUE` constraints</InternalLink> are validated locally in each cluster, therefore if a replicated write violates a `UNIQUE` constraint on the destination cluster (possibly because a conflicting write was already applied to the row) the replicating row will be applied to the DLQ.

For example, consider a table with a unique `name` column where the following operations occur in this order in a source and destination cluster running LDR:

On the **source cluster**:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- writes to the source table
INSERT INTO city (1, nyc); -- timestamp 1
UPDATE city SET name = 'philly' WHERE id = 1; -- timestamp 2
INSERT INTO city (100, nyc); -- timestamp 3
```

LDR replicates the write to the **destination cluster**:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- replicates to the destination table
INSERT INTO city (100, nyc); -- timestamp 4
```

*Timestamp 5:* <InternalLink path="architecture/glossary#range">Range</InternalLink> containing primary key `1` on the destination cluster is unavailable for a few minutes due to a <InternalLink path="cluster-setup-troubleshooting#network-partition">network partition</InternalLink>.

*Timestamp 6:* On the destination cluster, LDR attempts to replicate the row `(1, nyc)`, but it enters the retry queue for 1 minute due to the unavailable range. LDR adds `1, nyc` to the DLQ table after retrying and observing the `UNIQUE` constraint violation:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- writes to the DLQ
INSERT INTO city (1, nyc); -- timestamp 6
```

*Timestamp 7:* LDR continues to replicate writes:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- replicates to the destination table
INSERT INTO city (1, philly); -- timestamp 7
```

To prevent expected DLQ entries and allow LDR to be eventually consistent, we recommend:

* For **unidirectional** LDR, validate unique index constraints on the source cluster only.
* For **bidirectional** LDR, remove unique index constraints on both clusters.

## Step 1. Prepare the cluster

1. Enter the SQL shell for **both** clusters in separate terminal windows:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   cockroach sql --url "postgresql://root@{node IP or hostname}:26257?sslmode=verify-full" --certs-dir=certs
   ```
2. Enable the `kv.rangefeed.enabled` <InternalLink path="cluster-settings">cluster setting</InternalLink> on the **source** cluster:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SET CLUSTER SETTING kv.rangefeed.enabled = true;
   ```
3. On the **destination**, create a user with the <InternalLink path="security-reference/authorization#supported-privileges">`REPLICATION` system privilege</InternalLink> who will start the LDR job:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE USER {your username} WITH PASSWORD '{your password}';
   ```

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   GRANT SYSTEM REPLICATION TO {your username};
   ```

   If you need to change the password later, refer to <InternalLink path="alter-user">`ALTER USER`</InternalLink>.

## Step 2. Connect from the destination to the source

In this step, you'll set up an <InternalLink path="create-external-connection">external connection</InternalLink> from the destination cluster to the source cluster. Depending on how you manage certificates, you must ensure that all nodes between the clusters have access to the certificate of the other cluster.

You can use the `cockroach encode-uri` command to generate a connection string containing a cluster's certificate.

1. On the **source** cluster in a new terminal window, generate a connection string, by passing the replication user, node IP, and port, along with the directory to the source cluster's CA certificate:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   cockroach encode-uri {replication user}:{password}@{node IP}:26257 --ca-cert {path to CA certificate} --inline
   ```

   The connection string output contains the source cluster's certificate:

   ```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   {replication user}:{password}@{node IP}:26257?options=-ccluster%3Dsystem&sslinline=true&sslmode=verify-full&sslrootcert=-----BEGIN+CERTIFICATE-----{encoded certificate}-----END+CERTIFICATE-----%0A
   ```
2. In the SQL shell on the **destination** cluster, create an <InternalLink path="create-external-connection">external connection</InternalLink> using the source cluster's connection string. Prefix the `postgresql://` scheme to the connection string and replace <code>{'{source}'}</code> with your external connection name:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE EXTERNAL CONNECTION {source} AS 'postgresql://{replication user}:{password}@{node IP}:26257?options=-ccluster%3Dsystem&sslinline=true&sslmode=verify-full&sslrootcert=-----BEGIN+CERTIFICATE-----{encoded certificate}-----END+CERTIFICATE-----%0A';
   ```

## Step 3. Start LDR

In this step, you'll start the LDR job from the destination cluster. You can replicate one or multiple tables in a single LDR job. You cannot replicate system tables in LDR, which means that you must manually apply configurations and cluster settings, such as <InternalLink path="row-level-ttl">row-level TTL</InternalLink> and user permissions on the destination cluster.

1. From the **destination** cluster, start LDR. Use the fully qualified table name for the source and destination tables:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE LOGICAL REPLICATION STREAM FROM TABLE {database.public.source_table_name} ON 'external://{source_external_connection}' INTO TABLE {database.public.destination_table_name};
   ```

   If you would like to add multiple tables to the LDR job, ensure that the table name in the source table list and destination table list are in the same order:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE LOGICAL REPLICATION STREAM FROM TABLES ({database.public.source_table_name_1},{database.public.source_table_name_2},...)  ON 'external://{source_external_connection}' INTO TABLES ({database.public.destination_table_name_1},{database.public.destination_table_name_2},...);
   ```

   There are some tradeoffs between enabling one table per LDR job versus multiple tables in one LDR job. Multiple tables in one LDR job can be easier to operate. For example, if you pause and resume the single job, LDR will stop and resume for all the tables. However, the most granular level observability will be at the job level. One table in one LDR job will allow for table-level observability.

   Once LDR has started, an LDR job will start on the destination cluster. You can <InternalLink path="pause-job">pause</InternalLink>, <InternalLink path="resume-job">resume</InternalLink>, or <InternalLink path="cancel-job">cancel</InternalLink> the LDR job with the job ID. Use <InternalLink path="show-logical-replication-jobs">`SHOW LOGICAL REPLICATION JOBS`</InternalLink> to display the LDR job IDs:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SHOW LOGICAL REPLICATION JOBS;
   ```

   ```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
           job_id        | status  |          targets          | replicated_time
   ----------------------+---------+---------------------------+------------------
   1012877040439033857   | running | {database.public.table}   | NULL
   (1 row)
   ```

   If you're setting up bidirectional LDR, both clusters will have a history retention job and an LDR job running.

<Note>
  You cannot pause an LDR job for longer than 24 hours. LDR jobs paused for longer than 24 hours fail and cannot be recovered.
</Note>

1. Move on to [Step 4](#step-4-optional-set-up-bidirectional-ldr) to set up a second LDR job. Or, once you have set up your required LDR jobs, refer to [Step 5](#step-5-monitor-the-ldr-jobs) to monitor the jobs in the DB Console.

## Step 4. (Optional) Set up bidirectional LDR

At this point, you've set up one LDR job from cluster A as the source to cluster B as the destination. To set up LDR streaming in the opposite direction, complete [Step 1](#step-1-prepare-the-cluster), [Step 2](#step-2-connect-from-the-destination-to-the-source), and [Step 3](#step-3-start-ldr) again. Cluster B will now be the source, and cluster A will be the destination.

## Step 5. Monitor the LDR jobs

In this step, you'll access the <InternalLink path="ui-overview">DB Console</InternalLink> and monitor the status and metrics for the created LDR jobs. Depending on which cluster you would like to view, follow the instructions for either the source or destination.

<Tip>
  You can use the <InternalLink path="ui-overview">DB Console</InternalLink>, the SQL shell, <InternalLink path="datadog#enable-metrics-collection">Metrics Export</InternalLink> with Prometheus and Datadog, and <InternalLink path="child-metrics">labels with some LDR metrics</InternalLink> to monitor the job.

  For a full reference on monitoring LDR, refer to <InternalLink path="logical-data-replication-monitoring">Logical Data Replication Monitoring</InternalLink>.
</Tip>

1. Access the DB Console at `http://{node IP or hostname}:8080` and enter your user's credentials.
2. On the source cluster, navigate to the <InternalLink path="ui-jobs-page">**Jobs** page</InternalLink> to view a list of all jobs. Use the job **Type** dropdown and select **Replication Producer**. This will display the history retention job. This will run while the LDR job is active to protect changes to the table from <InternalLink path="architecture/storage-layer#garbage-collection">garbage collection</InternalLink> until they have been replicated to the destination cluster.
3. On the destination cluster, use the job **Type** dropdown and select **Logical Replication Ingestion**. This page will display the logical replication stream job. There will be a progress bar in the **Status** column when LDR is replicating a table with existing data. This progress bar shows the status of the initial scan, which backfills the destination table with the existing data.
4. On the destination cluster, click on **Metrics** in the left-hand navigation menu. Use the **Dashboard** dropdown to select <InternalLink path="ui-logical-data-replication-dashboard">**Logical Data Replication**</InternalLink>. This page shows graphs for monitoring LDR.

## What's next

* <InternalLink path="manage-logical-data-replication">Manage Logical Data Replication</InternalLink>: Manage the DLQ and schema changes for the replicating tables.
* <InternalLink path="create-logical-replication-stream">`CREATE LOGICAL REPLICATION STREAM`</InternalLink>
* <InternalLink path="data-resilience">Data Resilience</InternalLink>
