How To Configure REDIS In Magento 2 & Magento 1? (5 Minutes)

how to configure redis in magento 2 and magento 1

To begin with, there are several prerequisites for Redis cache configuration in Magento 1 & 2 which are:

  • Redis Server
  • PHP Redis Extension

Notes: The latest versions are more recommended.

Step 1: Update and Install Redis-server

wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
make test
sudo make install
cd utils
sudo ./install_server.sh
sudo service redis_6379 start
sudo update-rc.d redis_6379 defaults

Step 2: Configure Redis In Magento

A. Configure Magento 2 To Use Redis As Cache Storage

Add the following code to your app/etc/env.php:

<?php
// app/etc/env.php
return [
    // Other directives
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '0',
                    'port' => '6379',
                    'password' => ''
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '1',
                    'port' => '6379',
                    'compress_data' => '0',
                    'password' => ''
                ]
            ]
        ]
    ]
];

B. Configure Magento 2 To Use Redis As Session Storage

First, you need to locate the following snippet:

'session' => [
    'save' => 'files'
],
And replace it with snippet as follows:
'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379',
        'password' => '',
        'timeout' => '2.5',
        'persistent_identifier' => '',
        'database' => '2',
        'compression_threshold' => '2048',
        'compression_library' => 'gzip',
        'log_level' => '3',
        'max_concurrency' => '6',
        'break_after_frontend' => '5',
        'break_after_adminhtml' => '30',
        'first_lifetime' => '600',
        'bot_first_lifetime' => '60',
        'bot_lifetime' => '7200',
        'disable_locking' => '0',
        'min_lifetime' => '60',
        'max_lifetime' => '2592000'
    ] ],

Secondly, clear all the cache a session by running the following command:

bin/magento c:f
redis in magento 1

C. Configure Magento 1 To Use Redis

Firstly, go to your app/etc/local.xml, and change from:

<?xml version="1.0"?>
<config>
    <global>
        <install>
            <date><![CDATA[Tue, 04 Oct 2016 09:53:37 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[e972f3c4e8436052de805bdf1f40de0f]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[magento]]></username>
                    <password><![CDATA[magento]]></password>
                    <dbname><![CDATA[magento]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

To:

<?xml version="1.0"?>
<config>
    <global>
        // Other directives
        <!-- This is a child node of config/global -->
        <cache>
            <backend>Cm_Cache_Backend_Redis</backend>
            <backend_options>
                <server>127.0.0.1</server> <!-- or absolute path to unix socket -->
                <port>6379</port>                
                <database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
                <password></password> <!-- Specify if your server requires authentication -->
            </backend_options>
        </cache>
        <!--session_save><![CDATA[files]]></session_save-->
        <session_save>db</session_save>
        <redis_session>                       <!-- All options seen here are the defaults -->
            <host>127.0.0.1</host>            <!-- Specify an absolute path if using a unix socket -->
            <port>6379</port>
            <password></password>             <!-- Specify if your server requires authentication -->
            <timeout>2.5</timeout>            <!-- This is the connection timeout, not the locking timeout -->
            <db>0</db>                        <!-- Redis database number; protection from accidental loss is improved by using a unique DB number for sessions -->
            <compression_threshold>2048 </compression_threshold>  <!-- Set to 0 to disable compression (recommended when suhosin.session.encrypt=on); known bug with strings over 64k: https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues/18 -->
            <compression_lib>gzip</compression_lib>              <!-- gzip, lzf, lz4 or snappy -->
            <log_level>1</log_level>               <!-- 0 (emergency: system is unusable), 4 (warning; additional information, recommended), 5 (notice: normal but significant condition), 6 (info: informational messages), 7 (debug: the most information for development/testing) -->
            <max_concurrency>6</max_concurrency>                 <!-- maximum number of processes that can wait for a lock on one session; for large production clusters, set this to at least 10% of the number of PHP processes -->
            <break_after_frontend>5</break_after_frontend>       <!-- seconds to wait for a session lock in the frontend; not as critical as admin -->
            <fail_after>10</fail_after>                       <!-- seconds after which we bail from attempting to obtain lock (in addition to break after time) -->
            <break_after_adminhtml>30</break_after_adminhtml>
            <first_lifetime>600</first_lifetime>                 <!-- Lifetime of session for non-bots on the first write. 0 to disable -->
            <bot_first_lifetime>60</bot_first_lifetime>          <!-- Lifetime of session for bots on the first write. 0 to disable -->
            <bot_lifetime>7200</bot_lifetime>                    <!-- Lifetime of session for bots on subsequent writes. 0 to disable -->
            <disable_locking>0</disable_locking>                 <!-- Disable session locking entirely. -->
            <min_lifetime>60</min_lifetime>                      <!-- Set the minimum session lifetime -->
            <max_lifetime>2592000</max_lifetime>                 <!-- Set the maximum session lifetime -->
        </redis_session>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Secondly, clear all the caches of the session by running the following commands:

rm -rf /var/www/html/magento/var/session/*
rm -rf /var/www/html/magento/var/cache/*

Step 3: Restart and check the result

Next, you should restart as:

service redis-server restart

To check whether Redis-server is working or not, enter:

redis-cli ping

If the result is:

PONG

Then your server is responding.
Besides, you can monitor all the traffic by:

redis-cli monitor

then refresh the page. If you can see logs generating on your terminal, having random alphanumeric characters, which means that caching is working.
You can also check to see whether Redis-server is able to set keys or not:

redis-cli
127.0.0.1:6379> set mykey KEY
OK
127.0.0.1:6379> get mykey
"KEY"
127.0.0.1:6379> exit

Moreover, you can use the “info” command to get server information and statistics as:

redis-cli info

We have shown you some easy steps to configure Redis in both Magento 1 & 2. If you have any problems when following the tutorial, please leave a comment below.
  
Related Posts:

How To Clear Cache In Magento 2

How To Use Custom Local Storage & Cookie Storage In Magento 2?

How To Index Product Attributes With Source Model In Flat Table In Magento 2?

[Solved] Magento “One Or More Indexers Are Invalid” Error