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

tar xzf redis-stable.tar.gz
cd redis-stable
make test
sudo make install
cd utils
sudo ./
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:

// app/etc/env.php
return [
    // Other directives
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '',
                    'database' => '0',
                    'port' => '6379',
                    'password' => ''
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '',
                    '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' => '',
        '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"?>
            <date><![CDATA[Tue, 04 Oct 2016 09:53:37 +0000]]></date>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>


<?xml version="1.0"?>
        // Other directives
        <!-- This is a child node of config/global -->
                <server></server> <!-- or absolute path to unix socket -->
                <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 -->
        <redis_session>                       <!-- All options seen here are the defaults -->
            <host></host>            <!-- Specify an absolute path if using a unix socket -->
            <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: -->
            <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) -->
            <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 -->

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:


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> set mykey KEY
OK> get mykey
"KEY"> 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

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