A newer version of this documentation is available.

View Latest
March 16, 2025
+ 12

Initializing and running inter-Sync Gateway replication

Related inter-syncgateway topics: Overview | Run | Manage | Monitor | Conflict

Other related topics: Configuration Properties | Admin REST API

Context Clarification

This content relates only to inter-Sync Gateway replication in Sync Gateway 2.8+. For documentation on pre-2.8 inter-Sync Gateway replication (also known as SG Replicate) — see SG-Replicate

Introduction

Replications are initialized by submitting a replication definitionglossary icon using either:

  • A 'JSON' configuration file (sync-gateway-config.json)

  • The Admin REST API, using a utility such as curl, or an application such as Postman.

Wherever they are defined, the elements of a replication definition are the same, with the exception of these Admin REST API only elements:

  • adhoc — Use this to specify that the replication is ad hoc [1].

  • cancel — Use this to cancel on-going replications [1].

Example 1. Replication Characteristics Highlights
  • There are two types of replication: persistent and ad hoc (REST API only).

  • Replications of both types can run in one-shot or continuous replications modes.

  • All replications involve at least one local database.

  • Replications can be configured to purge documents when channel access is revoked (a removal notification is received).

  • Persistent continuous replications can be:

    • Reset — a checkpointglossary icon can be reset to zero

    • Updated — only the parameter values provided in the PUT request body will be updated

  • Persistent and ad hoc replications can be:

    • Removed — only the replication_id is needed to delete ongoing continuous or one-shot replications.

  • ENTERPRISE EDITION only:

    • Replications can use delta-sync mode, whereby only the changed data-items are replicated.

Replication Definition

All replications are 'initialized' by a replication definitionglossary icon in the configuration file or Admin REST API and operate within the context of a local database.

  • Configured replications use the database.{db-name}.replications property to add a replication definition to a local database.

  • REST API replications specify the local database and replication identity in the API POST/PUT request. Providing the replication definition parameters in the request body as a JSON string.

Both scenarios are covered in Example 2. It summarizes the replication definitionglossary icon elements[2], which are covered in more detail in Configuration Properties.

Database-level Settings

A number of database-level options are also especially relevant to Inter-Sync Gateway Replication, including:

  • sgreplicate_enabled — use this ENTERPRISE EDITION setting to allow the database to participate in Inter-Sync Gateway Replications.

  • this_db.delta_sync — use this setting to enable delta-sync replication on the database, it must be set if you want to use delta-sync in your replication definition.

  • sgreplicate_websocket_heartbeat_secs — use this setting to override the default (5 minute) heartbeat interval for websocket ping frames for this database.

  • sync — use this setting to specify the sync function logic — this is an essential part of access-control.

  • unsupported.sgr_tls_skip_verify — use this unsupported option to make development an testing easier by skipping verification of TLS certificates.

Replication-level Settings

Example 2. Replication Definition

This table summarize all the available configurable items. It includes a link to a detailed description of each.

Name and Link Summary

REST API ONLY
Use the Admin REST API’s adhoc parameter to specify that a replication is ad hoc rather than persistent.

Use batch_size to specify the number of changes to be included in a single batch during replication.

REST API ONLY
Use the Admin REST API’s cancel parameter only when you want to want to cancel an existing active replication.

Use conflict_resolution_type to specify how Sync Gateway should resolve conflicts. By default the automatic conflict resolution policy is applied.

Use continuous to specify whether this replication will run continuously, or be one-shot.

Use custom_conflict_resolver to provide the Javascript function used to resolve conflicts if "conflict_resolution_type": "custom".

Use direction to specify the replication is push, pull or pushAndPull relative to this node.

Use enable_delta_sync to specify use of delta sync for a replication.

Use filter to specify the name of the function to be used to filter documents.

Use max_backoff_time to specify the number of minutes Sync Gateway will spend trying to reconnect lost or unreachable remote targets.

Use purge_on_removal to specify (per replication) whether removing a channel should trigger a purge.

Use query_params to specify the key/value pairs to be passed to the filter named in filter.

Use remote to specify the database endpoint on the remote Sync Gateway custer.

Use replication_id to specify an identifying name for the replication.

Use initial_state to specify the state in which to launch the replication.

Generic Constraints

Replication

All active nodes in an active cluster must be running Sync Gateway version 2.8+.

ENTERPRISE EDITION

All replications are distributed evenly across available nodes. This means they cannot be guaranteed to run on the node from which they originate.

Access rights

The user running the replication must have read and write access to the data being replicated. This is not enforced by the system. Use your sync function to ensure a consistent approach is applied across all clusters.

Mixing Inter-Sync Gateway Replication Versions

Versions of inter-Sync Gateway replications pre- and post-2.8 can legitimately be in use at the same time, especially during transition. However, you should avoid initializing identical pre-2.8 (SG Replicate) and 2.8+ replications.

Running Configured Replications

Replications in the configuration file start automatically whenever Sync Gateway is (re)started. Unless you inhibit this by adding an "initial_state": "stopped" parameter to the replication definition — see: initial_state. You can manually start 'stopped' replication using Starting a replication.

Example 3. Configured Replications — Continuous and One-shot
//  . . . other configuration entries
"db1-rep-id1-pull-cont":
  "replication_id": "db1-rep-id1-pull-cont",
  "direction": "pull",
  "continuous": true (1)
  "purge-on-removal": true,
  "remote": "http://user:password@example.com:4985/db1-remote",
  "filter":"sync_gateway/bychannel",
  "query_params": {
    "channels": ["channel1.user1"]
  }
//  . . . other configuration entries
1 Make this a continuous replication that remains running, listening for changes to process. Because it is also persistent, it will start automatically following Sync Gateway node restarts (state defaults to running).

Running Admin REST API Replications

Replications initialized by sending a POST, or PUT, request to the _replication endpoint will start running automatically, unless the "initial_state": "stopped" parameter is specified. with a JSON object defining the replication parameters — as shown in Example 4.

  • You can run multiple replications simultaneously with different replication topologies, provided both databases being synchronized have the same sync function.

You can submit requests using the curl utility (as in these examples) or an application such as Postman.

Example 4. Submitting API Requests

This example initializes a persistent, continuous, replication between a local database and one on a remote Sync Gateway instance.

json
curl --location --request POST 'http://localhost:4985/db1-local/_replication/' \ --header 'Content-Type: application/json' \ --dataraw '{ "replication_id": "db1-rep-id1-pull-cont", "direction": "pull", "continuous": true (1) "purge-on-removal": true, "remote": "http://user:password@example.com:4985/db1-remote", "filter":"sync_gateway/bychannel", "query_params": { "channels": ["channel1.user1"] } }'

1. This parameter is not available in the configuration file.
2. The definitions apply to configured and API replications).