Redis and Load balancer to Build a High Available Web Application Environment

Redis is an opensource distributed caching engine and in-memory data structure store. It can be used as a database, cache and a message broker. It also improves the availability of the applications during the outages.

PHP Session Handling By using Redis

1. Architecture

2. Installing and Configuring Redis

i) Installing Redis

ii) Configuring Redis

3. Installing and Configuring PHP-Redis In Webserver

i) Installing PHP-Redis

ii) Making Redis as a session handler

iii) Verifying the operation

1. Architecture

In the following architecture Web Servers attached under a Load Balancer to route the web traffic from the client to an available webserver. The Redis server is also configured to cache PHP sessions. The application’s cache data is stored in the Redis.


2. Installing and Configuring Redis

i ) Installing Redis

Install this service by running the following commands in the SSH terminal

sudo yum -y install epel-release
sudo yum -y install redis

After the installation start and enable the service in the boot time by running the following commands

sudo systemctl start redis.service  
sudo systemctl enable redis 
Expected Output:
Created symlink from /etc/systemd/system/ to /usr/lib/systemd/system/redis.service.

Check the service status by the following command

sudo systemctl status redis
Expected Output:
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
   Active: active (running) since Sun 2020-02-09 06:38:43 UTC; 2min 19s ago
 Main PID: 1737 (redis-server)
   CGroup: /system.slice/redis.service
           └─1737 /usr/bin/redis-server

Feb 09 06:38:43 test-cent systemd[1]: Starting Redis persistent key-value database...
Feb 09 06:38:43 test-cent systemd[1]: Started Redis persistent key-value database.

ii) Configuring Redis

By default, Redis didn’t allow remote connections, it can be connected only through the host where the Redis service is running. For allowing remote access to this server, add the Private IP address of the Redis host in the configuration file(/etc/redis.conf) as follows,

sudo vi /etc/redis.conf. Search for “bind” and then add the Private IP of the Redis machine. After Editing it will look like below mentioned,

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.

Restart the service for changes, by as follows

sudo systemctl restart redis.service  

Use the following netstat command to check the service is listening on your private interface

netstat  -lp | grep redis
Expected Output:
tcp        0      0 test-cent:6379*               LISTEN      2678/redis-server 1
tcp        0      0 localhost:6379*               LISTEN      2678/redis-server 1

3. Installing and Configuring PHP-Redis In Webserver

i ) Installing PHP-Redis

PHP-Redis Extension is required for PHP to communicate with the Redis Server. So install that extension by using following Linux commands,

Adding PHP repo by as follows,
rpm -Uvh  
rpm -Uvh  
yum -y install yum-utils  
yum-config-manager --enable remi-php70
Note: If you already added required PHP repos, skip this repo adding step
Install PHP-Redis by as follows,
yum -y install php-redis

Check the Installation by using following command,
php -m | grep redis

Expected Output:
[[email protected] ~]# php -m | grep redis

Do the same installation in Web-Server-2 and verify the Installation

ii) Making Redis as a session handler

Open and edit the php.conf file as follows,

sudo vi /etc/httpd/conf.d/php.conf

 i) Change the  "php_value session.save_handler" parameter value from "file" to "redis"
 ii) Change the "php_value session.save_path" parameter value from "/var/lib/php/session" to " "tcp://<Redis-Server-IP>:6379". (Example:  "tcp://")
 iii) Restart the httpd service by as follows,
sudo service httpd restart
  iv) Do the Same Configuration in Web-Server-2 and restart httpd service.

iii) Verifying the Operation

Open the ssh terminal of the Redis server and launch the Redis CLI by using the following command,

Expected Output:
[[email protected] ~]# redis-cli>

Check the active PHP sessions in the Redis server by executing “keys *” command in redis-cli.

Expected Output:
[[email protected] ~]# redis-cli> keys *
(empty list or set)>

The session list is now empty, We will create a session and check the PHP session handling operation in Redis by as follows,

a) Go to the browser and open the application by URL. Here two servers are connected to a load balancer to route the traffic, so I am using the following URL http://<load-balancer-ip/application-path ( and log in using my credentials.

Log IN Page
Log In Succes Page

b) I am successfully logged into my PHP application using my credentials. Now the PHP session was created. Verify if the session is successfully stored in the Redis server by as follows,

c) Open redis-cli and execute the “keys *” command, the active PHP sessions will be listed in this cli.

Expected Output:


Now the session handling using Redis is verified. In this architecture, if the web-server-1 becomes unavailable after the login session created for the user, the load balancer routes the traffic to web-server-2 and session data will be retrieved from the Redis server. So the user session will be maintained.


  1. In this Web Server architecture, both the web servers and the Redis server is placed in the same Virtual network. So the private of the Redis server used as a Bind Ip address and specific Inbound security rules to allow communication between webservers and Redis server is not Required.
  2. If the Web servers and Redis server placed in a different Virtual network, in Redis configuration file(/etc/redis.conf) the Bind address should be to allow communication from webservers and Redis server’s security group rules and firewall rules should have the configuration for denying the traffic from unwanted hosts and that should allow the connection only from the required web servers.

For High Available Mongo Cluster Deployment –