Recently I was googling about how to start multiple Redis instances and was a bit confused of different approaches I found. It’s not a simple task if you’re not a DevOps engineer. So, let’s put everything in place.
Looking ahead, in this article I’ll be using Systemd approach of starting a service on Linux server.
Why running multiple Redis instances is beneficial
While Redis supports multiple databases, it’s single threaded at the same time. Therefore, if you need to use Redis for multiple applications or microservices on the same host, it’s better to have multiple Redis instances. Otherwise, if you just need to separate different keys belonging to the same application you’re good to go with multiple databases approach. But, as for me, multiple Redis databases bring you more complexity and issues with scalability in the future.
Systemd, System V Init, OpenRC, runit, etc.
The approach of how a Redis instance is started depends of the init system that is used on your Linux host. To understand which init system is used on your server just take a look to which binary a symbolic link /sbin/init references. For example, in my case it’s Systemd:
$ ls -la /sbin/initlrwxrwxrwx 1 root root 20 Jun 29 2022 /sbin/init -> /lib/systemd/systemd
Alright, well if your /sbin/init points to /lib/systemd/systemd this article is for you :)
Systemd approach, in 4 steps
Next I will focus on how to start a new Redis instance in the scope of Systemd init system. Systemd is used by default in many Linux distributions like Debian, Ubuntu, Red Hat, OpenSUSE, Fedora, CentOS, CoreOS, and more.
I’m going to start a new Redis instance on port 6378, but you can select a different port in your case.
sudo
In bash commands below I will not specify sudo, but if the system will tell you that you don’t have enough permissions to run a command — just prefix the command with sudo and run it again.
1. /etc/redis/redis.conf
Let’s say we already have one running Redis instance and we’d like to start another one on port 6378. First, let’s copy the default redis.conf file to redis-6378.conf:
cp /etc/redis/redis.conf /etc/redis/redis-6378.confThen edit redis-6378.conf with your favourite editor (I hope it’s Vim), and change port, pidfile, logfile and dbfilename settings. In my case they look like this:
port 6378
pidfile /var/run/redis/redis-server-6378.pid
logfile /var/log/redis/redis-server-6378.log
dbfilename dump-6378.rdbAlso, check that the file redis-6378.conf has the correct owner and group. They have to be “redis redis” like in the output below:
$ ls -la /etc/redis/redis-6378.conf-rw-r----- 1 redis redis 46947 Oct 1 10:56 /etc/redis/redis-6378.conf
If you have different values, just use the chown command to change them:
chown redis:redis /etc/redis/redis-6378.conf2. /etc/systemd/system/redis-6378.service
Systemd service files are usually stored in /lib/systemd/system/, but if the custom Systemd service is created it’s better to put it into /etc/systemd/system/, and I’m going to follow this recommendation. Let’s make a copy of /lib/systemd/system/redis-server.service and put it into /etc/systemd/system/redis-server-6378.service:
cp /lib/systemd/system/redis-server.service /etc/systemd/system/redis-server-6378.serviceThen we’re going to edit /etc/systemd/system/redis-server-6378.service, customising Description, ExecStart, PIDFile and Alias settings:
Description=Advanced key-value store on port 6378
ExecStart=/usr/bin/redis-server /etc/redis/redis-6378.conf
PIDFile=/var/run/redis/redis-server-6378.pid
Alias=redis-6378.serviceNow let’s check that the redis-server-6378.service file has the right owner and group. They have to be “root root” like in the output below:
$ ls -la /etc/systemd/system/redis-server-6378.service-rw-r--r-- 1 root root 1183 Oct 1 10:25 /etc/systemd/system/redis-server-6378.service
If you have a different owner or group, use this command to fix it:
chown root:root /etc/systemd/system/redis-server-6378.service3. /var/lib/redis/dump-6378.rdb
While the new redis instance hasn’t been activated, you can copy the database file from another one, if you need it. Otherwise the redis instance will be started with an empty database.
cp /var/lib/redis/dump.rdb /var/lib/redis/dump-6378.rdbAfter copying check that dump-6378.rdb’s owner and group is “redis redis”like in the output below:
$ ls -la /var/lib/redis/dump-6378.rdb-rw-rw---- 1 redis redis 206365 Sep 27 10:07 /var/lib/redis/dump-6378.rdb
If you have different values, just use the chown command to fix it:
chown redis:redis /var/lib/redis/dump-6378.rdb4. Start Systemd Redis service
First, let’s check the status of our newly created Systemd redis service:
$ systemctl status redis-server-6378.serviceredis-server-6378.service - Advanced key-value store on port 6378
Loaded: loaded (/etc/systemd/system/redis-server-6378.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: http://redis.io/documentation,
man:redis-server(1)
Here we see that Systemd loaded our service automatically, it means that it’s ready to be enabled or started. For now, it’s disabled and inactive.
There’s the difference between enabling and starting a service in Systemd.
Enabling and disabling a Systemd service don’t actually start or stop it, they control what happens at boot, so only at boot time the enabled/disabled status matters. So, if the Linux host is rebooted, the enabled service will be started.
So, let’s enable our service:
systemctl enable redis-server-6378.serviceAnd then start it:
systemctl start redis-server-6378.serviceTest if Redis server is running
Let’s do a quick test if our new Redis instance has been started successfully:
$ redis-cli -a 'this_is_my_redis_password' -p 6378 pingPONG
Or, if you configured to run a Redis server without a password:
$ redis-cli -p 6378 pingPONG
Alternatively, you can find it running in system processes:
$ ps aux | grep redisredis 19328 0.0 0.4 40456 4148 ? Ssl 11:03 0:25 /usr/bin/redis-server 0.0.0.0:6378
Troubleshooting
If something went wrong, first it’s better to check the status of Redis service:
systemctl status redis-server-6378.serviceNext, you can check logs for the Redis service (if the output is empty try to run this command with sudo):
journalctl -u redis-server-6378.serviceAlso, you can get a list of Systemd services with statuses:
systemctl list-units --type=serviceOr even list all the Systemd unit files:
systemctl list-unit-filesIn my case, the info from the commands above helped me to understand and fix issues I faced.
Useful commands for Systemd services
Here are some useful Systemd commands that you can just copy/paste and run. In examples below, I use my redis-server-6378.service, but you can replace it with your Systemd service name.
Get status of a service:
systemctl status redis-server-6378.serviceEnable a service, so it will be started on boot:
systemctl enable redis-server-6378.serviceDisable a service, so it will NOT be started on boot:
systemctl disable redis-server-6378.serviceStart a service:
systemctl start redis-server-6378.serviceStop a service:
systemctl stop redis-server-6378.serviceRestart a service (stop + start):
systemctl restart redis-server-6378.service