Magento 2 - Multiple Redis instances cache and session storage backends

REMI rpm repository providing the latest versions of the PHP stack, full featured, and some other software, to the Fedora and Enterprise Linux RHEL, CentOS, Oracle, Scientific Linux users. It mainly contains (nearly) vanilla versions. Command to install the Remi repository configuration package:

yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm 

 To install latest redis, memcached and php extensions:

yum --enablerepo=remi, remi-php72 -y install redis memcached php-pecl-redis php-pecl-memcache php-pecl-lzf 

Configure multiple redis instances on port 6379 and 6380:

systemctl disable redis
cat > /etc/systemd/system/redis@.service <<END
[Unit]
Description=Redis %i
After=network.target
OnFailure=service-status-mail@%n.service
PartOf=redis.target
[Service]
Type=simple
User=redis
Group=redis
PrivateTmp=true
PIDFile=/var/run/redis-%i.pid
ExecStart=/usr/bin/redis-server /etc/redis-%i.conf
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target redis.target
END
 
cat > /etc/systemd/system/redis.target <<END
[Unit]
Description=Redis start/stop all redis@.service instances
END

for REDISPORT in 6379 6380
do
mkdir -p /var/lib/redis-${REDISPORT}
chmod 755 /var/lib/redis-${REDISPORT}
chown redis /var/lib/redis-${REDISPORT}
cp -rf /etc/redis.conf /etc/redis-${REDISPORT}.conf
chmod 644 /etc/redis-${REDISPORT}.conf
sed -i "s/^bind 127.0.0.1.*/bind 127.0.0.1/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^dir.*/dir \/var\/lib\/redis-${REDISPORT}\//"  /etc/redis-${REDISPORT}.conf
sed -i "s/^logfile.*/logfile \/var\/log\/redis\/redis-${REDISPORT}.log/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^pidfile.*/pidfile \/var\/run\/redis-${REDISPORT}.pid/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^port.*/port ${REDISPORT}/" /etc/redis-${REDISPORT}.conf
sed -i "s/dump.rdb/dump-${REDISPORT}.rdb/" /etc/redis-${REDISPORT}.conf
done

systemctl daemon-reload
systemctl enable redis@6379
systemctl enable redis@6380

Configure Magento 2 cache and session storage backends:

su ${MAGE_WEB_USER} -s /bin/bash -c "bin/magento setup:config:set \
--cache-backend=redis \
--cache-backend-redis-server=127.0.0.1 \
--cache-backend-redis-port=6380 \
--cache-backend-redis-db=1 \
-n"
## page cache
su ${MAGE_WEB_USER} -s /bin/bash -c "bin/magento setup:config:set \
--page-cache=redis \
--page-cache-redis-server=127.0.0.1 \
--page-cache-redis-port=6380 \
--page-cache-redis-db=2 \
-n"
## session
su ${MAGE_WEB_USER} -s /bin/bash -c "bin/magento setup:config:set \
--session-save=redis \
--session-save-redis-host=127.0.0.1 \
--session-save-redis-port=6379 \
--session-save-redis-log-level=3 \
--session-save-redis-db=1 \
--session-save-redis-compression-lib=snappy \
-n"