REDIS WITH PHP APPLICATION

Story

A website/application has own challenges even it is successed, when it touches high number of users.
similarly we faced one issue that an application is reciving huge users at few times but not all times.
obviously we planned  application much more reliable and consistent. If the term scalability crossed your mind, you've got the right idea.

scalability is the ability of a system to handle an increased amount of traffic or processing and accommodate growth while maintaining a desirable user experience.
There are basically two ways of scaling a system: vertically, also known as scaling up,(increasing system configuration with memory and hard disk i can say)
and horizontally, also known as scaling out.(just deploying another server to share the exisitng load)

vertically scaling has no issue but it has it own challenges like only mem or infra will be changed its not suitable for all scenarios.

we have problem here when we do harizonal scaling

When user A makes a request to mydomain.com, the load balancer will forward requests to server1. User B, on the other hand, gets forwarded another node from the cluster, server2.

What happens when user A makes changes to the application, like uploading files or updating content in the database? How do you maintain consistency across all nodes in the cluster? Further, PHP saves session information in disk by default. If user A logs in, how can we keep that user's session in subsequent requests, considering that the load balancer could send them to another server in the cluster?

Need to maintain user session in common storage devise then only it can have more accessability


****************REDIS IS SOLUTION FOR THIS********

Redis.
Redis is an in-memory data structure store, used as a database, cache and message broker. It stores data in key-value which gives Redis more flexibility and performance.

Prerequisites
Before we start this tutorial you need to have complete access to your server we need to install Redis server and also add some required PHP extension.

Installing and configure Redis
Step 1. Login to your server via terminal or putty if you are using the window.



Step 2. Once login successfully runs bellow commands.

sudo apt update
sudo apt install redis-server
Step 3. Check if Redis is installed successfully using below command

sudo systemctl status redis
Configure Redis sever with PHP
Step 4. Now we can check Redis server is functioning correctly, we can use redis-cli, Redis CLI is a command-line tool for Redis.

redis-cli
Redis cache with PHP



Step 5. Now if you want to restart Redis server run this command

sudo systemctl restart redis
Or, if you want to clear Redis cache you can run this command

redis-cli flushall
Configure Redis for PHP extension
To use Redis with your PHP application we need an extension that connects PHP with Redis to do that we need to run below command.

sudo pecl install redis
sudo apt-get install php-redis
After this, we need to restart our PHP using below command

sudo service php7.0-fpm restart
Now once your PHP restarts you are ready to use the Redis server.


***first method to add cache as redis***

ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ pwd
/etc/php/7.2/cli
ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ grep  session.save_handler php.ini
session.save_handler = files
ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ ###MAKE THIS ENTRY AS redis###
ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ session.save_handler = redis
session.save_handler: command not found
ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ grep session.save_path php.ini
;     session.save_path = "N;/path"
;     session.save_path = "N;MODE;/path"
;session.save_path = "/var/lib/php/sessions"
;       (see session.save_path above), then garbage collection does *not*
ubuntu@ip-172-31-37-25:/etc/php/7.2/cli$ hostname
ip-172-31-37-25




****second method to add cache as REDIS***
Add Redis as Cache in LAVAPHP 3 Application
If you want to use Redis as a cache for your LAVAPHP 3 Application you update your cache setting in config/app.php and update your cache setting with this

'Cache' => [
        'default' => [
            'className' => 'Redis',
            'path' => CACHE,
            'password' => false,
            'server' => '127.0.0.1',
            'port' => 6379,
        ],
        '_cake_core_' => [
            'className' => 'Redis',
            'prefix' => 'cake_redis_core_',
            'path' => CACHE . 'persistent/',
            'serialize' => true,
            'duration' => '+1 years',
            'server' => '127.0.0.1',
            'port' => 6379,
            'password' => false,
        ],
        '_cake_model_' => [
            'className' => 'Redis',
            'prefix' => 'cake_redis_model_',
            'path' => CACHE . 'models/',
            'serialize' => true,
            'duration' => '+1 years',
            'server' => '127.0.0.1',
            'port' => 6379,
            'password' => false,
        ],
    ],






No comments: