Redis Wranglin’

Introduction

I recently did some maintenance on a few machines running Redis, including version upgrades. The one contingency I had to accomodate was that we use multiple databases above the number in the default configuration and I didn’t want to loose this data. I didn’t know what would happen to the data from extra databases if Redis was launched with fewer, but with the method below I didn’t have to find out.

The information below can be found here and there about the net; it’s here mostly for my own reference. If it turns out ot be of use to someone else, all the better.

At the time of writing, the latest recommended version of Redis was 2.8.12. I chose to upgrade from repositories rather than building from source.

Upgrading on Ubuntu 12.04

The version in the default repositories for this release, and the one I had installed was 2.2.12.

There’s a PPA that stays up-to-date with latest Redis releases. It’s here. Let’s add it.

$ sudo add-apt-repository ppa:chris-lea/redis-server
$ sudo apt-get update

Stop Redis and do the upgrade.

$ sudo /etc/init.d/redis-server stop
$ sudo apt-get upgrade redis-server

At this point I was given an option to keep my old configuration file and I elected to do so. The upgrade completed and Redis failed to start because this file was incompatible with the new version. Fine by me - I didn’t want it to start anyway.

I replaced my old configuration with the one that comes in the distribution.

$ sudo mv /etc/redis/redis.conf /etc/redis/redis.conf.old
$ sudo cp /etc/redis/redis.conf.dpkg-dist /etc/redis/redis.conf

Then I just set the databases in redis.conf back to the desired number and restarted Redis. All my data was preserved.

$ sudo /etc/init.d/redis-server start

Upgrading on CentOS 6.5

As it turns out, the experience is much the same as on Ubuntu.

For this platform, the Remi repository has up-to-date packages.

Install repository, then edit the repository file to enable it.

$ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
$ sudo vim /etc/yum.repos.d/remi.repo

Edit it so that the enabled setting reads like this.

enabled=1

Check that the Remi repository is now enabled. It should appear in the list.

$ sudo yum repolist

Double check that the upstream version is the one we want (2.8.12).

$ sudo yum list redis

Stop Redis and do the upgrade.

$ sudo service redis stop
$ sudo yum update redis

Redis won’t start automatically like for Ubuntu. As before, we copy the new configuration file.

$ sudo mv /etc/redis.conf /etc/redis.conf.old
$ sudo cp /etc/redis.conf.rpmnew /etc/redis.conf

Edit the new config as required and restart Redis.

$ sudo service redis start

Running a Second Redis Instance

There are varied methods for achieving this to be found online. What follows is a combination of some plus my own intuition. I’m going to refer to my new Redis service as redis-cache. This was done on CentOS 6.5.

First make a folder for the data and set redis as the owner.

$ sudo mkdir /var/lib/redis-cache
$ sudo chown redis:redis /var/lib/redis-cache

Make a copy of the existing config to use for our new service.

$ sudo cp /etc/redis.conf /etc/redis-cache.conf

In this file there are four specific settings that need to change. I’ll leave finding them as an exercise for the reader, but in condensed form, here are what the edited lines will look like.

pidfile /var/run/redis-cache.pid
port 6380
logfile /var/log/redis/redis-cache.log
dir /var/lib/redis-cache/

Create a new lock file.

$ sudo touch /var/lock/subsys/redis-cache

Now we need a script to start the service, so let’s copy the existing init script.

$ sudo cp /etc/init.d/redis /etc/init.d/redis-cache

Next edit the script so that it will run our new service. In condensed form as before, three lines should be found and changed to look like these.

pidfile="/var/run/redis/redis-cache.pid"
REDIS_CONFIG="/etc/redis-cache.conf"
lockfile=/var/lock/subsys/redis-cache

Ensure the service is set to start automatically (optional) and fire it up.

$ sudo chkconfig --add redis-cache
$ sudo chkconfig redis-cache on
$ sudo service redis-cache start

You can connect to it with redis-cli by specifying the port.

$ redis-cli -p 6380

That’s it. Redis wrangled.