Logo
Englika

How to run multiple Redis instances

How to run multiple Redis instances

In spite of Redis supports multiple databases, it's more convenient to run a separate instance of Redis for each database, so a database would have its own dump file and config. To archive that, you can run Redis either in different docker containers or in different systemd units. Let's consider the second way.

Frankly speaking, it's too simple to write about it, but unfortunately, the instructions in most articles don't work if you connect to Redis using a unix socket. Even if you are going to use a TCP port, it's not a good idea to, for example, store the configs for different Redis instances in the same directory /etc/redis.

It's safer to keep different Redis instances in separate directories, including the pid, unixsocket, logfile, dir (working directory), and the config too, especially since "redis-server can write to its own config file when in cluster mode" (the quote from redis-server.service).

Let's prepare 2 separate directories for a new instance of Redis with the same permissions and owners.

# For storing a config
mkdir /etc/redis-rspamd
chown redis:redis /etc/redis-rspamd
chmod 2770 /etc/redis-rspamd

# For storing a dump
mkdir /var/lib/redis-rspamd
chown redis:redis /var/lib/redis-rspamd
chmod 0750 /var/lib/redis-rspamd

To create a new instance of Redis you should just create a new Redis config and a systemd unit with minor changes.

Copy your Redis config.

cp -p /etc/redis/redis.conf /etc/redis-rspamd/redis.conf

Change in the file /etc/redis-rspamd/redis.conf the following lines.

pidfile /run/redis-rspamd/redis-server.pid
unixsocket /run/redis-rspamd/redis.sock
dir /var/lib/redis-rspamd
# logfile /var/log/redis-rspamd/redis-server.log

If you connect to Redis using TCP port, change it here as well.

Copy the systemd unit.

cp -p /lib/systemd/system/redis-server.service /lib/systemd/system/redis-server-rspamd.service

Change in the file /lib/systemd/system/redis-server-rspamd.service the following lines.

ExecStart=/usr/bin/redis-server /etc/redis-rspamd/redis.conf
PIDFile=/run/redis-rspamd/redis-server.pid
RuntimeDirectory=redis-rspamd
ReadWriteDirectories=-/var/lib/redis-rspamd
ReadWriteDirectories=-/var/log/redis-rspamd
ReadWriteDirectories=-/run/redis-rspamd
ReadWriteDirectories=-/etc/redis-rspamd
Alias=redis-rspamd.service

Reload systemd daemon.

systemctl daemon-reload

Ensure the new instance of Redis is started and enabled on boot.

systemctl start redis-rspamd
systemctl enable redis-rspamd

That's all. The second instance of Redis stores everything in other directories and doesn't have permissions to the first instance (see ReadOnlyDirectories and ReadWriteDirectories in the final file redis-server-rspamd.service). You can create as many Redis instances as you want the same way.

It's important to note that if you don't change RuntimeDirectory, executing systemctl stop redis-rspamd leads to the disappearance of the unix socket of the first instance of Redis because "runtime directory is cleaned up automatically after use", so when you stop the second instance, it leads to remove the runtime directory of the first instance. At the same time, systemctl status redis and pid aux | grep redis will say you that everything is fine – the systemd unit of the first instance of Redis is active and the process is running, but there will be no unix socket, in spite of you stopped only the second instance of Redis. That's why it's crucial to change RuntimeDirectory for each instance of Redis as well as ReadWriteDirectories.

Related posts

How to get N rows per group in SQL

Let's assume that you are developing the home page in an online store. This page should display product categories with 10 products in each. How do you make a query to the database? What indexes do you need to create to speed up query execution?

How to get N rows per group in SQL