Skip to content

Instantly share code, notes, and snippets.

@Paprikas
Forked from doejoe13/notes.md
Created April 13, 2019 07:23
Show Gist options
  • Save Paprikas/ef55f5b2401c4beec75f021590de6a67 to your computer and use it in GitHub Desktop.
Save Paprikas/ef55f5b2401c4beec75f021590de6a67 to your computer and use it in GitHub Desktop.
How to run multiple Redis instances on Ubuntu 18.04

Create the directory for the new instance

$ sudo install -o redis -g redis -d /var/lib/redis2

Create a new configuration file

$ sudo cp -p /etc/redis/redis.conf /etc/redis/redis2.conf

Edit the new configuration file

$ sudo nano /etc/redis/redis2.conf
pidfile /var/run/redis2/redis-server2.pid
logfile /var/log/redis/redis-server2.log
dir /var/lib/redis2
port 6380

Create new service file

$ sudo cp /lib/systemd/system/redis-server.service /lib/systemd/system/redis-server2.service

Edit the new service file

$ sudo nano /lib/systemd/system/redis-server2.service
ExecStart=/usr/bin/redis-server /etc/redis/redis2.conf
PIDFile=/var/run/redis/redis-server2.pid
RuntimeDirectory=redis2
ReadWriteDireRuntimeDirectory=redis2
ReadWriteDirectories=-/var/run/redis2
Alias=redis2.service

Enable and start the service

$ sudo systemctl enable redis-server2.service
$ sudo systemctl start redis-server2.service

Check status

$ ps aux |grep redis
@ShadowMonster
Copy link

ShadowMonster commented Sep 24, 2021

There is small mistake. Pid file path should be: PIDFile=/var/run/redis2/redis-server2.pid in service file

@AntonLugtenburg
Copy link

Hi, there is some mistake in this line here right ?
ReadWriteDireRuntimeDirectory=redis2

@simptive
Copy link

System service should be modified without changing directory. Here's the difference that works:
@@ -8 +8 @@
-ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
+ExecStart=/usr/bin/redis-server /etc/redis/redis-cache-server.conf
@@ -10 +10 @@
-PIDFile=/run/redis/redis-server.pid
+PIDFile=/run/redis/redis-cache-server.pid
@@ -46 +46 @@
-Alias=redis.service
+Alias=redis-cache-server.service

@oxilor
Copy link

oxilor commented Apr 15, 2024

System service should be modified without changing directory. Here's the difference that works: @@ -8 +8 @@ -ExecStart=/usr/bin/redis-server /etc/redis/redis.conf +ExecStart=/usr/bin/redis-server /etc/redis/redis-cache-server.conf @@ -10 +10 @@ -PIDFile=/run/redis/redis-server.pid +PIDFile=/run/redis/redis-cache-server.pid @@ -46 +46 @@ -Alias=redis.service +Alias=redis-cache-server.service

If the directory RuntimeDirectory has not been changed, executing systemctl stop redis2 leads to the disappearance of the unix socket and the pid file of the first instance of Redis because "runtime directory is cleaned up automatically after use". At the same time, systemctl status redis and pid aux | grep redis says that everything is fine - the systemd unit is active and the process exists, but there is no unix socket of the first instance of Redis. That's why it's crucial to change RuntimeDirectory. ReadWriteDirectories should be changed for different directories as well.

BTW, I think it's better to store everything in different directories: 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". It's safer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment