Cross Datacenter Replication (XDCR) in Couchbase provides an easy way to replicate data from one cluster to another. The clusters are typically set in geographically
diverse data centers. This enables for disaster recovery or to bring data closer to users for faster data access. This blog will show:

  • Setup two data centers using Docker Swarm
  • Run Couchbase containers on each node of Docker Swarm
  • Setup a Couchbase cluster on each Docker Swarm cluster
  • Configure one-way XDCR between two Couchbase clusters

For the purpose of this blog, the two data centers will be setup on a local machine using Docker Machine.
Complete code used in this blog is available at:

Create Consul Discovery Service

Each node in Docker Swarm needs to be registered with a discovery service. This blog will use Consul for that purpose. And even Consul will be running on a Docker Machine. Typically, you’ll run
a cluster of Consul but for simplicity a single instance is started in our case. Create a Docker Machine and start Consul using this script:

Create Docker Swarm cluster

Docker Swarm allows multiple Docker hosts to be viewed as a single unit. This allows your multi-container applications to easily run on multiple hosts. Docker Swarm serves the same Remote API as served by a single host. This allows your existing tools to target a single host or a cluster of hosts. Both the Docker Swarm clusters will be registered with a single discovery service. This is achieved by using the following value for

Create a Docker Swarm cluster using Docker Machine using this script:

The script needs to be invoked as:

This will create two Docker Swarm clusters with one “master” and two “worker” as shown below:

Consul is running on Docker Machine with IP address And so Consul UI is accessible at


It shows two Docker Swarm clusters that have been registered. Exact list of nodes for each cluster can also be seen. Nodes in clusterA are shown: swarm-xdcr-consul-clusterA

Nodes in clusterB are shown:

Run Couchbase containers

Run Couchbase container on each node of Docker Swarm cluster using this Compose file.

Configure Docker CLI for the first cluster and run 3 containers:

Check the running containers:

Configure Docker CLI for the second cluster and run 3 containers:

Check the running containers:

Create/Rebalance Couchbase cluster

Scaling and Rebalancing Couchbase Cluster using CLI explains how to create a cluster of Couchbase nodes and rebalance an existing cluster using Couchbase CLI.
Create Couchbase cluster on each Swarm cluster using this script.

The script needs to be invoked as:

And now rebalance this cluster using this script:

This script is invoked as:

Couchbase Web Console for any node in the cluster will show the output:

Invoke this script to create the second Couchbase cluster as:

Rebalance this cluster as:

Couchbase Web Console for any node in the second cluster will show the output: swarm-xdcr-couchbase-clusterB

Setup XDCR

Cross datacenter replication can be setup to be uni-directional, bi-directional or multi-directional. Uni-directional allows data to replicated from source cluster to destination cluster, bi-directional allows replication both ways, multi-directional
allows to configure in any direction. We’ll create a simple uni-directional replication using this script:

This script is invoked as:

A bi-directional replication can be easily created by executing the commands again but reversing the source and destination cluster. Couchbase Web Console for the source cluster will show:

Couchbase Web Console for the destination cluster will show:


This blog shows how you can simplify your complex deployments using Docker Machine, Docker Swarm, and Docker Compose.

Posted by Arun Gupta, VP, Developer Advocacy, Couchbase

Arun Gupta is the vice president of developer advocacy at Couchbase. He has built and led developer communities for 10+ years at Sun, Oracle, and Red Hat. He has deep expertise in leading cross-functional teams to develop and execute strategy, planning and execution of content, marketing campaigns, and programs. Prior to that he led engineering teams at Sun and is a founding member of the Java EE team. Gupta has authored more than 2,000 blog posts on technology. He has extensive speaking experience in more than 40 countries on myriad topics and is a JavaOne Rock Star for three years in a row. Gupta also founded the Devoxx4Kids chapter in the US and continues to promote technology education among children. An author of several books on technology, an avid runner, a globe trotter, a Java Champion, a JUG leader, NetBeans Dream Team member, and a Docker Captain, he is easily accessible at @arungupta.

Leave a reply