Upgrading Redis with zero downtime
This tutorial explains, upgrading Redis With Zero Downtime
Scenario
Let Suppose a Scenario where we have a Redis Server (Version: Redis-4.0.9) running on a Server with Redis Port 6388 and we want upgrading Redis to the latest version( 5.0.8) without any downtime.Current location of existing installed Redis is /usr/local/bin/redis-server
Also Read : Install Redis on Linux from source
&& How to install Jenkins on AWS EC2
Workaround
We will follow the following steps upgrading Redis Server without downtime.
1) Install the latest version(5.0.8) of Redis from source on the Same Server with a different installation location(/opt/local) without doing any changes on existing Redis Server.
2) Configure a different Redis Port 6399 on the newly Installed latest-Version Redis Server.
3) Make the existing old version- 4.0.9 Redis-Server as Master and newly installed Latest-Version-5.0.8 Redis Server as a slave.
4)Verify the Master-Slave setup
5) Promote the SLAVE as Master.
6) Verify the new Master Server
7) Change the Redis Port on your Application.
Let us perform the above steps in detail:
Step 1-Install the latest version(5.0.8) of Redis from source
Step 1.1– Open the following Redis download link on the browser.
https://redis.io/download
Right click on the Download 5.0.8 under Stable(5.0) version or whatever the latest stable version and click on “copy link location”
Step 1.2– Download the latest Redis version copied in previous step on your Linux Box using wget command.
$ cd ~
$ wget http://download.redis.io/releases/redis-5.0.8.tar.gz
$ tar -xvf redis-5.0.8.tar.gz
$ cd redis-5.0.8/
$ make
Step 1.3– If you get any error in running make command ,run the following command.
For Centos
$ sudo yum groupinstall ‘Development Tools’
$ sudo yum install gcc make
For Ubuntu
$ sudo apt-get install build-essential
$ sudo apt install gcc make
Step 1.4 – Run make Install with a different location. Here I put /opt/local .
$ make PREFIX=/opt/local install
Step 2- Configure a different Redis Port 6399 on the newly Redis Server.
$ cd ~/redis-5.0.8/utils/
Run following script to configure redis.
$ ./install_server.sh
As soon as you run the script the following prompts will appear.
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6399 —-> Enter port as 6399
Please select the redis config file name [/etc/redis/6399.conf] ————> Press Enter
Selected default – /etc/redis/6399.conf ————> Press Enter
Please select the redis log file name [/var/log/redis_6399.log] ———— > Press Enter
Selected default – /var/log/redis_6399.log ————> Press Enter
Please select the data directory for this instance [/var/lib/redis/6399] ————> Press Enter
Selected default – /var/lib/redis/6399 ————> Press Enter
Please select the redis executable path [/opt/local/bin/redis-server] ————> Press Enter
Selected config:
Port : 6399
Config file : /etc/redis/6399.conf
Log file : /var/log/redis_6399.log
Data dir : /var/lib/redis/6399
Executable : /opt/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6399.conf => /etc/init.d/redis_6399
Installing service…
Success!
Starting Redis server…
Installation successful!
Start redis Service
$ sudo service redis_6399 start
or
$/etc/init.d/redis_6399 start
$ sudo service redis_6399 status
or
$ /etc/init.d/redis_6399 status
Step 3 – Create Master-Slave Setup
Master Configuration
Open the Redis config file of existing Redis Server that we have to make as Master
$ vi /etc/redis/6388.conf
Look for the string ‘bind’ and make sure it should be as :
bind 0.0.0.0
Also look for the sting ‘appendonly’ and ‘appendfilename’ and change the setting as :
appendonly yes
appendfilename “appendonly.aof”
save your configuration and exit.
Slave Configuration
Open the newly installed redis config file.
$ vi /etc/redis/6399.conf
Look for the string ‘bind’ and make sure it should be as :
bind 0.0.0.0
Add the following line in the 6399.conf file and save the file.
SLAVEOF 127.0.0.1 6388
After saving restart the Slave Redis Server.
$/etc/init.d/redis_6399 restart
Step 4- Verify the Master-Slave setup
To check if master and slave setup is working properly. Connect and run following command on Master and Slave Redis one by one.
On Master Redis
Connect the redis using following command and run command info
$/usr/local/bin/redis-cli -p 6388
127.0.0.1:6388> info
Scroll down to Replication portion of Info and you can see the role as master.
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6388,state=online,offset=5026,lag=0
master_replid:7baac8da12544712db01e982975744c41ee1241e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5026
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5026
Now Create a test key value on MASTER to check if data is Replicating to SLAVE.
127.0.0.1:6388> set test “testing”
Now to check the key value on master run ‘get test’
127.0.0.1:6388> get test
“testing”
On SLAVE Redis
Connect the SLAVE Redis using following command and run command info
$/usr/local/bin/redis-cli -p 6399
127.0.0.1:6399> info
Scroll down to Replication portion of Info and you can see the role as slave.
# Replication
role:slave
master_host:127.0.0.1
master_port:6388
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:5659
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7baac8da12544712db01e982975744c41ee1241e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5659
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5659
Here some points that we need to notice are as follows:
master_link_status should be up
master_last_io_seconds_ago should greater than or equal to 1
master_sync_in_progress should be 0
Now check the key created on Master is replicated to SLAVE or not by running command ‘get test’
127.0.0.1:6399> get test
“testing”
The above result shows that Key value is replicated to SLAVE.
Step 5– Promote the SLAVE as Master.
Connect to the SLAVE SERVER and run the following command.
$/usr/local/bin/redis-cli -p 6399
127.0.0.1:6399> SLAVEOF NO ONE
OK
Now run info command to check if SLAVE promoted as Master.
127.0.0.1:6399> info
# Replication
role:master
connected_slaves:0
master_replid:6bb6786fb2adc3ee060052451ae8d66318151b87
master_replid2:7baac8da12544712db01e982975744c41ee1241e
master_repl_offset:8248
second_repl_offset:8189
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:8248
Here you can see in replication section that the role is changed to MASTER.
Step 6- Verify the new Master Server
Connect your New master and check if you are able to create key.
$/usr/local/bin/redis-cli -p 6399
127.0.0.1:6399> set test1 “testing”
OK
127.0.0.1:6399> get test1
OK
The above results show that you are able to create key now as the New Redis server is promoted as master. If you are still not able to create a test key on master, mean you are still in read-only mode(SLAVE MODE), in that case try to restart the Redis service of new MASTER(6399) and try again.
Step 7- Change the Redis Port on your Application.
Change the Redis Port no from 6388 to 6399 in your Application or ask your Developer to do the same.
I hope you enjoyed this tutorial and learned Upgrading Redis with zero downtime . If you think this is really helpful, please do refer my blog posts to others as well. Also, please give your valuable feedback in the comment box.I will always happy to help resolve your queries anytime.
Thank You
If you think we helped you or just want to support us, please consider these:-
Connect to us: Facebook | Twitter
Hi, thanks for writing this! I’m curious why do we need to set the previous master as append-only? Did you put that to ease sync between slave-master? Or is there any other reason?
HI Fori,
Thanks for your comment !! This is to ease sync only.
Can you help to point me to a resource which supports this argument? I’m having trouble to find doc explaining relation of master-slave sync with append-only mode