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.

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/multi-user.target.wants/redis.service 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
           └─limit.conf
   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 127.0.0.1:6379

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,

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 10.1.2.5

# 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          0.0.0.0:*               LISTEN      2678/redis-server 1
tcp        0      0 localhost:6379          0.0.0.0:*               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 https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm  
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
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://10.1.2.5:6379")
 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,

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

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

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

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 (http://23.96.86.103/loginsystem/admin/) and log in using my credentials.

Redis
Log IN Page
Redis
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:

Redis

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.

Note:

  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 0.0.0.0 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 – https://pyronoidninja.com/mongodb-cluster-creation-with-high-availability/

Reference:

https://redislabs.com/

https://www.digitalocean.com/

https://phpgurukul.com