top of page

Redis Cluster and Master-Slave Replication

Redis is an open-source, in-memory data store that can be used as a database, cache, or message broker. Redis supports various data structures and is known for its high performance and low-latency data access. Redis can be used in a master-slave replication setup, which provides data redundancy and scalability.


A Redis Cluster and Master-Slave Replication configuration generally includes one or more Redis master nodes and one or more Redis slave nodes. This is how it works:


  1. Master Node: The master node is the primary Redis server where data is initially written. It is responsible for handling write operations and maintaining the dataset. You can have multiple master nodes in a Redis Cluster for high availability and load distribution.

  2. Slave Node: A slave node is a read-only copy of the data from the master node. It replicates the data from the master node and can be used for read operations. Slave nodes are useful for scaling read operations and providing failover support in case the master node goes down. You can also have multiple slave nodes for each master node.


So lets begin setting up Redis Cluster with Master-Slave configuration. Following are the IP address details of Master & Slave node


Server Details :

  • Master Node : 192.168.1.2

  • Slave Node : 192.168.1.3

Prerequisites :

  • Both master & slave node should have same version of Redis

  • Both master & slave node should be able to connect each other over the port 6379

Step 1: Configure Redis Master Node.

On the master node set the following parameters under /etc/redis.conf and restart the redis service.

[siddhesh@redismaster ~]# sudo egrep '^bind|^protected-mode|^requirepass' /etc/redis.conf
bind 127.0.0.1 -::1 192.168.1.2
protected-mode no
requirepass Sid@123
[siddhesh@redismaster ~]# 

Step 2: Configure Redis Slave Node.


On the slave node set the following parameters under /etc/redis.conf and restart the redis service.

[siddhesh@redisslave ~]# sudo egrep '^bind|^protected-mode|^requirepass|^masterauth|^slaveof' /etc/redis.conf
bind 127.0.0.1 192.168.1.3
protected-mode no
slaveof 192.168.1.2 6379
masterauth Sid@123
requirepass Sid@123
[siddhesh@redisslave ~]#

Step 3: Restart Redis Service on Master & Slave Node.

[siddhesh@redismaster ~]# sudo systemctl restart redis
[siddhesh@redisslave ~]# sudo systemctl restart redis

Step 4: Now, on the Master Node, create a test Hash key and test Replication.

[siddhesh@redismaster ~]# sudo for i in {1..10}; do redis-cli -a Sid@123 hset testhashreplication:key$i field$i value$i; done
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
(integer) 1
[siddhesh@redismaster ~]# sudo redis-cli -a Sid@123 keys testhashreplication*
 1) "testhashreplication:key6"
 2) "testhashreplication:key1"
 3) "testhashreplication:key4"
 4) "testhashreplication:key9"
 5) "testhashreplication:key10"
 6) "testhashreplication:key8"
 7) "testhashreplication:key7"
 8) "testhashreplication:key2"
 9) "testhashreplication:key3"
10) "testhashreplication:key5"
[siddhesh@redismaster ~]#

On the master, we created a total of ten hash type keys. Let's check the slave's status to find out if it's synchronised or not.


Step 5: Let's check the slave's status to find out if it's synchronised or not.

[siddhesh@redisslave ~]# sudo redis-cli -a Sid@123 keys testhashreplication*
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 1) "testhashreplication:key9"
 2) "testhashreplication:key2"
 3) "testhashreplication:key6"
 4) "testhashreplication:key1"
 5) "testhashreplication:key7"
 6) "testhashreplication:key4"
 7) "testhashreplication:key10"
 8) "testhashreplication:key3"
 9) "testhashreplication:key8"
10) "testhashreplication:key5"
[siddhesh@redisslave ~]#

As you can see, we now have similar data synced in real time on Slave.

Step 6: Check Master Cluster Status

[siddhesh@redismaster ~]# sudo redis-cli -a Sid@123 INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.3,port=6379,state=online,offset=23934,lag=0
master_failover_state:no-failover
master_replid:f7179369260760aff66c68bce3f5cb46b078608c
master_replid2:7f198375968b9a63857a50bc1bf5a6487e87fe41
master_repl_offset:23948
second_repl_offset:21917
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:21917
repl_backlog_histlen:2032
[siddhesh@redismaster ~]#

In this output:

The Redis server is acting as a master (role:master).

One slave is connected (connected_slaves:1).

The master_replid is the replication ID of the master.

slave0 is the connected slave server with respective IP addresses and port.


Step 7: Check Slave Cluster Status

[siddhesh@redisslave ~]# sudo redis-cli -a Sid@123 INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:24284
slave_repl_offset:24284
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f7179369260760aff66c68bce3f5cb46b078608c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:24284
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:21945
repl_backlog_histlen:2340
[siddhesh@redisslave ~]#

In this output :

The Redis server is acting as a slave (role:slave).

It is connected to a master server at IP 192.168.1.2 and port 6379.

The connection to the master is "up," and the last interaction was 0 seconds ago.

Synchronization with the master is not in progress (master_sync_in_progress:0).

The replication offset on the slave is 24284.

The slave is in read-only mode (slave_read_only:yes).

No additional slaves are connected (connected_slaves:0).


תגובות

דירוג של 0 מתוך 5 כוכבים
אין עדיין דירוגים

הוספת דירוג
bottom of page