Quickly Test xCluster Active-Active Multi-Master Replication with yugabyted

xCluster replication is YugabyteDB’s implementation of asynchronous replication for disaster recovery. 

It allows you to set up one or more unidirectional replication flows between universes.

A cross-cluster (xCluster) deployment provides asynchronous replication across two data centers or cloud regions.

If you want to try out this variant of xCluster replication to understand how it works, you don’t need any sort of complicated infrastructure (i.e. two multi-region, multi-node YugabtyeDB Universes).

With yugabyted, you can “kick the tires” of even the most complex option for xCluster, an Active-Active Multi-Master deployment!

All you need is your laptop or a single VM!

Example:

(Make sure you download and install YugabyteDB first!)

Note: In the example we will create two single node clusters using the loopback addresses 127.0.0.1 for cluster 1 and 127.0.0.2 for cluster 2. 

Create cluster 1 and a table to replicate:

				
					[root@localhost ~]# yugabyted start --advertise_address 127.0.0.1 --base_dir /root/yb-cluster/c1 > c1.log

[root@localhost ~]# sleep 10

[root@localhost ~]# ysqlsh -h 127.0.0.1 -c "CREATE TABLE employee (id INT PRIMARY KEY, name VARCHAR, badge_num VARCHAR UNIQUE);"
CREATE TABLE

				
			

Note: I used the Linux sleep command to give yugabyted a little extra time to become fully ready to receive connections.

Create cluster 2 and a table to replicate (same table in cluster 1) :

				
					[root@localhost ~]# yugabyted start --advertise_address 127.0.0.2 --base_dir /root/yb-cluster/c2 > c2.log

[root@localhost ~]# sleep 10

[root@localhost ~]# ysqlsh -h 127.0.0.2 -c "CREATE TABLE employee (id INT PRIMARY KEY, name VARCHAR, badge_num VARCHAR UNIQUE);"
CREATE TABLE
				
			

Get the Universe UUID of Cluster 1 (from the log file):

				
					[root@localhost ~]# grep "Universe UUID" c1.log | sed 's/.*: //; s/|.*//' # Cluster 1 UUID
b7597446-3bae-44b0-b285-3e7a4e4dda21
				
			

Get the Universe UUID of Cluster 2 (from the log file):

				
					[root@localhost ~]# grep "Universe UUID" c2.log | sed 's/.*: //; s/|.*//' # Cluster 2 UUID
037f88fc-921c-41bf-b472-a672ab1d3eab
				
			

Get the Table ID of the EMPLOYEE table in Cluster 1:

				
					[root@localhost ~]# ysqlsh -h 127.0.0.1 -Atc "SELECT '0000' || lpad(to_hex(d.oid::int), 4, '0') || '00003000800000000000' || lpad(to_hex(c.oid::int), 4, '0') FROM pg_class c, pg_namespace n, pg_database d WHERE c.relname = 'employee' AND c.relnamespace = n.oid AND d.datname = current_database();"
000033f5000030008000000000004000
				
			

Get the Table ID of the EMPLOYEE table in Cluster 2:

				
					[root@localhost ~]# ysqlsh -h 127.0.0.2 -Atc "SELECT '0000' || lpad(to_hex(d.oid::int), 4, '0') || '00003000800000000000' || lpad(to_hex(c.oid::int), 4, '0') FROM pg_class c, pg_namespace n, pg_database d WHERE c.relname = 'employee' AND c.relnamespace = n.oid AND d.datname = current_database();"
000033f5000030008000000000004000
				
			

Set up xCluster replication / Source Cluster C1 — > Target Cluster C2:

				
					[root@localhost ~]# yb-admin -master_addresses 127.0.0.2:7100 setup_universe_replication b7597446-3bae-44b0-b285-3e7a4e4dda21_c1 127.0.0.1:7100 000033f5000030008000000000004000
Replication setup successfully
				
			

Set up xCluster replication / Source Cluster C2 — > Target Cluster C1:

				
					[root@localhost ~]# yb-admin -master_addresses 127.0.0.1:7100 setup_universe_replication 037f88fc-921c-41bf-b472-a672ab1d3eab_c2 127.0.0.2:7100 000033f5000030008000000000004000
Replication setup successfully
				
			

Now, I will insert some data into the EMPLOYEE table on cluster 1 (127.0.0.1) and observe that the data is replicated to cluster 2 (127.0.0.2):

				
					[root@localhost ~]# ysqlsh -h 127.0.0.1 -c "INSERT INTO employee (id, name, badge_num) VALUES (1, 'Luis', 'ABC'), (2, 'Mari', 'DEF'), (3, 'Jim', 'GHI');"
INSERT 0 3

[root@localhost ~]# ysqlsh -h 127.0.0.1 -c "SELECT * FROM employee ORDER BY id;"
 id | name | badge_num
----+------+-----------
  1 | Luis | ABC
  2 | Mari | DEF
  3 | Jim  | GHI
(3 rows)

[root@localhost ~]# ysqlsh -h 127.0.0.2 -c "SELECT * FROM employee ORDER BY id;"
 id | name | badge_num
----+------+-----------
  1 | Luis | ABC
  2 | Mari | DEF
  3 | Jim  | GHI
(3 rows)
				
			

Next, I will insert some data into the EMPLOYEE table on cluster 2 (127.0.0.2) and observe that the data is replicated to cluster 1 (127.0.0.1):

				
					[root@localhost ~]# ysqlsh -h 127.0.0.2 -c "INSERT INTO employee (id, name, badge_num) VALUES (4, 'Josh', 'JKL'), (5, 'Jane', 'MNO'), (6, 'Helen', 'PQR');"
INSERT 0 3

[root@localhost ~]# ysqlsh -h 127.0.0.2 -c "SELECT * FROM employee ORDER BY id;"
 id | name  | badge_num
----+-------+-----------
  1 | Luis  | ABC
  2 | Mari  | DEF
  3 | Jim   | GHI
  4 | Josh  | JKL
  5 | Jane  | MNO
  6 | Helen | PQR
(6 rows)

[root@localhost ~]# ysqlsh -h 127.0.0.1 -c "SELECT * FROM employee ORDER BY id;"
 id | name  | badge_num
----+-------+-----------
  1 | Luis  | ABC
  2 | Mari  | DEF
  3 | Jim   | GHI
  4 | Josh  | JKL
  5 | Jane  | MNO
  6 | Helen | PQR
(6 rows)
				
			

Have Fun!

Note: All of the yb-admin commands for xCluster set up are documented here!

Lucy Lounging