Redis Cluster backup and restore.

Redis is an in-memory cache or key-value store. You can save the keys and values and access them very fast as it resides in memory. You can also persist the data in the disk as it provides an option for the same.

One of the most famous ways to use Redis is its clustering mode. In clustering mode, Redis divides the data in shards and then saves the particular keys in different shards and maintains multiple replicas of that shard.

Redis Cluster backup and restore.

In this article, we will try to see how you can take a backup of a Redis cluster and restore it to a new Redis cluster. Let’s start Redis Cluster backup and restore

We will divide this article into 3 parts. First, we will try to create a cluster next copy the data third start the new cluster.

Creating a Redis cluster

Here we will be first launching cluster on port 7000,7001,7002,7003,7004 and 7005. Follow the below steps for the same.

Create 5 directories 7000, 7001, 7002, 7003, 7004 and 7005

Now create a file redis.conf in each of the directories with the content as below.

port 7000 #for different directory change the port as the directory name 
cluster-enabled yes #work in cluster node
cluster-config-file nodes.conf #where to save node conf
cluster-node-timeout 5000 #when to say node is not reachable.
save 60 10 #after how much second or keys update to save to file. 

Next, we will launch these Redis nodes. Open 6 terminals and run the below command you can also run them in the backend.

In terminal 1
cd 7000 && redis-server redis.conf
In terminal 2
cd 7001 && redis-server redis.conf
In terminal 3
cd 7002 && redis-server redis.conf
In terminal 4
cd 7003 && redis-server redis.conf
In terminal 5
cd 7004 && redis-server redis.conf
In terminal 6
cd 7005 && redis-server redis.conf

Now we will have 6 Redis nodes running but they are not in cluster mode. For them to join the cluster run the below command in a new terminal.

redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
--cluster-replicas 1

Now you will be able to see the Redis nodes working in clusters.

You can run the commands to see the cluster-info.

cluster info #you can see cluster state
cluster nodes #you can see node information

Try and put some keys in these like below

redis-cli -h 127.0.0.1 -p 7000 -c
set test test

This will put the key in the cluster. Next, we will stop the cluster and create a new cluster in ports 7010, 7011, 7012, 7013, 7014, 7015

Coying the data

Stop the nodes after 60 seconds as we have config to take a backup every 60 seconds.

Next, when you stop the nodes in each directory you will find the following files.

nodes.conf 
redis.conf
dump.rdb

nodes.conf and dump.rdb are the two files that we need to save to create a new cluster.

Copy these files in the new directories with name 7000 to 7010, 7001 to 7011…

cp 7000/dump.rdb 7010/
cp 7001/dump.rdb 7011/
cp 7002/dump.rdb 7012/
cp 7003/dump.rdb 7013/
cp 7004/dump.rdb 7014/
cp 7005/dump.rdb 7015/
cp 7000/nodes.conf 7010/
cp 7001/nodes.conf 7011/
cp 7002/nodes.conf 7012/
cp 7003/nodes.conf 7013/
cp 7004/nodes.conf 7014/
cp 7005/nodes.conf 7015/

Start the new cluster.

Next, create redis.conf in each directory like below

port 7010
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
save 60 10

Run below commands in 6 terminals

In terminal 1
cd 7010 && redis-server redis.conf
In terminal 2
cd 7011 && redis-server redis.conf
In terminal 3
cd 7012 && redis-server redis.conf
In terminal 4
cd 7013 && redis-server redis.conf
In terminal 5
cd 7014 && redis-server redis.conf
In terminal 6
cd 7015 && redis-server redis.conf

Now you will be able to see cluster running but the master and slave not in sync because these have not the same address as below in this scenario you have to instruct them to join the cluster. Run the below commands to do the same.

redis-cli -h 127.0.0.1 -p 7010 cluster meet 127.0.0.1 7015
redis-cli -h 127.0.0.1 -p 7011 cluster meet 127.0.0.1 7015
redis-cli -h 127.0.0.1 -p 7012 cluster meet 127.0.0.1 7015
redis-cli -h 127.0.0.1 -p 7013 cluster meet 127.0.0.1 7015
redis-cli -h 127.0.0.1 -p 7014 cluster meet 127.0.0.1 7015

Now you can see cluster-info to see the cluster status.

cluster info #you can see cluster state
cluster nodes #you can see node information

Next, you can test the cluster by getting the key you have setup.

redis-cli -h 127.0.0.1 -p 7010
get test

You can also find the above article below.

This is all in Redis Cluster backup and restore. If you like the article please share and subscribe.

Also, we have the new version of our app up and running please check it out at
https://play.google.com/store/apps/details?id=com.learnsteps.Learnsteps


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

1 COMMENT
  • Yash Mane
    Reply

    Hi Gaurav,
    I am trying to do the same but no luck,
    I am doing back-up and restore in redis-cluster running in kubernetes as pods, I have 6 pods and have configured as cluster but when i copy the dump.rdb file into the pod and restart the pod the file gets override. can you help me out here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.