Upgrading redis... not as difficult as you may think

Published: September 16, 2019
Sidekiq 6 requires redis 4 or later. Some distros don't ship with that version so an upgrade is required.

I have side project running on a VPS. Specifically, centos 7. It's a pretty standard Rails app using sidekiq for background processing. I want to upgrade sidekiq to version 6; however, it requires redis 4 or later. Unfortunately, centos 7 only comes with redis 3. So, a major upgrade of redis is required.

For this app, redis provides caching and the sidekiq queues. While could live with wiping my existing redis database if required, I did want to at least back it up. Selfishly, I want to try to keep my existing total counts for sidekiq, if possible....

This is how I did it.

Step one, I wanted to figure out how to at least back up the redis db. Redis databases are actually just a single file (or two if appendonly is enabled). The majority of the time, you will find the database file(s) in /var/lib/redis. But, to make sure, you can ask redis for this information by issuing the config get dir command to the cli...

$ redis-cli
127.0.0.1:6379> auth MYPASSWORD # If your redis db is password protected
127.0.0.1:6379> config get dir
1) "dir"
2) "/var/lib/redis"

So, now we know where the database lives.

$ ls /var/lib/redis
dump.rdb

In my case, I do not have appendonly enabled. Therefore, I only have one file to backup. If you do have it enabled, you may see an aof file as well as the rdb file. To backup your redis database, simply cp the files to wherever you wish. Depending on how recent you want your backup to be relative to upgrading the cluster, you probably will want to hold off on doing this until the last minute. For me, being off by a few minutes is fine, so I backed up the database just in case.

Now for actually upgrading redis. For centos7, I found a repo which provides the latest redis server. For other distros, you'll need to find what you need to find. Obviously you should vet any external source of packages before using them. In my case, I went with the Remi repository.

sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

When the time came to do the upgrade, I enabled the repo with yum and installed redis. Since redis was already installed, the Remi repo provided an updated version, so yum upgraded the package.

$ sudo yum --enablerepo=remi install redis
...
Resolving Dependencies
 --> Running transaction check
 ---> Package redis.x86_64 0:5.0.5-1.el7.remi will be upgraded
...

The upgrade happened just like any other package in centos. Now all that leaves is actually restarting the redis server.

During all this, the app and sidekiq are still running. While both Rails and sidekiq should be "smart" enough to reconnect to redis if the connection is interrupted, I opted for a brief period of downtime and shut both down. At this point, nothing should be reading or writing to the redis database. I took this time backup the rds file.

Finally, I figure either this will work or it won't... and issued a restart to redis.

$ systemctl restart redis

No errors and the redis process started back up. I brought up the Rails app it was responsive. I brought sidekiq back up and it started processing jobs as expected. A quick check of the sidekiq web ui and...

Redis showing the new version. Jobs being received and processed as normal. Upgrade complete. And, I got to keep my existing stats!

So, unlike things such as postgresql or mongodb, upgrading redis seems pretty straight forward and easy, at least for me. For cases where redis is running in a cluster, that upgrade is probably a little more involved. That said, the database file appears to be compatible between major versions (at least versions 3 to 5) so I would think a similar path of update binary, back up database file just in case, and restarting the daemon for each node should be enough. Obviously, your miles may vary.

Hope this helps anyone needing to do a similar upgrade!

Tags: redis