December, 11 2019

Using Multiple Instances of Redis For Your Laravel Application

I worked as a Laravel backend developer at Qwickpage for a year. Qwickpage has lots of products that are still in their development stage but its is primarily a social network for entrepreneurs.

While at Qwickpage we have two instances of the core application running on the server. One is for development while the other is served live (production).

We make use of Redis as our caching system for the two instances of the app and the end result was a conflict.

The two applications (dev & live) were using the same instance of Redis on the server, each one keeps overriding the cached data of the other and returning wrong cached data.

How Did We Fix This

We fixed this by creating another instance of Redis that runs on port 6380. So the live app takes port 6379 while dev app takes 6380; We now have two instances of Redis running on our server.

In this post, I will show you how you can create another instance of Redis on your Linux server. I will assume your already have Redis installed on your Linux server.

Now Lets Go!

To create another instance of Redis we need to duplicate the existing Redis configuration file:

cp /etc/redis/redis_6379.conf /etc/redis/redis_6380.conf

Update port 6380 config file (etc/redis/redis_6380.conf) like this:

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

Create a port 6380 working directory:

mkdir /var/lib/redis/6380

Update port 6379 (/etc/init.d/redis_6379) service script from this:

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"

to this:

NAME=`basename ${0}`
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/${NAME}.pid
CONF="/etc/redis/${NAME}.conf"
REDISPORT="${NAME#*_}"

Create a symlink script for port 6380 instance

ln -s /etc/init.d/redis_6379 /etc/init.d/redis_6380

To start the two Redis server instances run this:

/etc/init.d/redis_6379 start
/etc/init.d/redis_6380 start

You can now update the dotenv files of your two applications and assign them different Redis port respectively.

Cheers!

You can also follow me on twitter @stephenjude_

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now