Running Multiple Redis Instances

Using systemd to Spawn Multiple Copies of a Service

Mauro Morales
Apr 25, 2016 · 3 min read

This article will teach you how to run one or more Redis instances on a Linux server using systemd to spawn copies of a service.

Installing Redis

The easiest way to install Redis in Linux is with your distributions package manager. Here is how you would do it on openSUSE:

sudo zypper install redis

In case your distribution doesn’t provide a Redis package, you can always follow these instructions to compile it from scratch.

Configuring a Redis instance

Once installed you will need to create a configuration file for each instance of the service you want to run.

1. Make a copy of the example/default file that is provided by the package.

cd /etc/redis/
cp default.conf.example my_app.conf

2. Change the ownership of the newly created configuration file to user “root” and group“redis”.

chown root.redis my_app.conf

3. Edit “my_app.conf”.

Add a “pidfile”, a “logfile” and a “dir”.

pidfile /var/run/redis/my_app.pid
logfile /var/log/redis/my_app.log
dir /var/lib/redis/my_app/

Make sure the “daemonize” option is set to “no” (this is the default value).

daemonize no

Define a “port” number.

port 6379

4. Save the configuration file.

5. Create the database directory at the location given in the configuration file.

install -d -o redis -g redis -m 0750 /var/lib/redis/my_app

Repeat the steps in Configure a Redis instance for every instance you want to set up. In my case I set up a second instance called “my_other_app”

.
├── default.conf.example
├── my_app.conf
└── my_other_app.conf

Adding Units to systemd for the Redis service

In order for systemd to know how to enable and start each instance individually you will need to add a service unit inside the system configuration directory located at “/etc/systemd/system”. For convenience you might also want to start/stop all instances at once. For that you will need to add a target unit.

1. Create the service unit file “redis@.service” with the following contents:

[Unit]
Description=Redis
After=network.target
PartOf=redis.target
[Service]
Type=simple
User=redis
Group=redis
PrivateTmp=true
PIDFile=/var/run/redis/%i.pid
ExecStart=/usr/sbin/redis-server /etc/redis/%i.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target redis.target

2. Create the target unit file “redis.target” with the following contents:

[Unit]
Description=Redis target allowing to start/stop all redis@.service instances at once

Interacting with Redis

If everything went as expected you should be able to interact with the individual instances:

systemctl start redis@my_app
systemctl enable redis@my_other_app

And also with all the instances at the same time:

systemctl restart redis.target
systemctl stop redis.target

Troubleshooting

If things didn’t go as expected and you cannot start the instance make sure to check the instance’s status:

systemctl status redis@my_app

If the issue doesn’t show up there then check systemd’s journal:

journalctl -u redis@my_app

For example if you forgot to give the right permissions to the configuration file you’d see something like this inside the journal:

Apr 23 10:02:53 mxps redis-server[26966]: 26966:C 23 Apr 10:02:53.917 # Fatal error, can’t open config file ‘/etc/redis/my_app.conf’

Acknowledgments

  • Thanks to the openSUSE Redis package maintainers for creating such a nice package that you can learn from it.
  • The book How Linux Works provided the details on how systemd instances work.

If you found this article useful please give it some ❤ and if you have any questions or feedback please leave a comment.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store