Implementing Redis on Magento 1

We recommend using the Colin Mollenhour Redis session and Redis cache extensions. These extensions typically use Modman for deployment, but here we document a simple process to install without Modman.

We'll assume Magento is already installed and that you are installing this on www.example.com

cd /microcloud/domains/example/domains/example.com/http

First install the Redis cache and session extensions using the automated installer

/microcloud/scripts_ro/redis_install.sh

After the installer has run, it will prompt you an entry to insert in your crontab, you can edit it using the following

crontab -e

Now the extensions are installed, you just need to add the respective configuration to the Magento ./app/etc/local.xml

Just after <global> add, change persistent and db to suit.

        <cache>
          <backend>Cm_Cache_Backend_Redis</backend>
          <backend_options>
            <server>redis11.i</server>
            <port>6379</port>
            <persistent></persistent>
            <database>0</database>
            <password></password>
            <force_standalone>0</force_standalone>
            <connect_retries>3</connect_retries>
            <read_timeout>10</read_timeout>
            <automatic_cleaning_factor>0</automatic_cleaning_factor>
            <compress_data>1</compress_data>
            <compress_tags>1</compress_tags>
            <compress_threshold>20480</compress_threshold>
            <compression_lib>lzf</compression_lib>
          </backend_options>
        </cache>

        <session_save>db</session_save>
        <redis_session>             
            <host>redis1.i</host>  
            <port>6379</port>
            <timeout>2.5</timeout>  
            <persistent></persistent>
            <db>0</db>
            <compression_threshold>2048</compression_threshold> 
            <compression_lib>lzf</compression_lib> 
            <log_level>4</log_level>
            <log_broken_locks>0</log_broken_locks>            
            <max_concurrency>10</max_concurrency>
            <break_after_frontend>5</break_after_frontend>
            <break_after_adminhtml>30</break_after_adminhtml>            
            <first_lifetime>600</first_lifetime> 
            <bot_first_lifetime>60</bot_first_lifetime>
            <bot_lifetime>3600</bot_lifetime>
            <disable_locking>1</disable_locking>
            <min_lifetime>60</min_lifetime>
            <max_lifetime>86400</max_lifetime>
        </redis_session>

If you are using Enterprise Edition, you can also store FPC in Redis using the following (placed after <global>)

        <full_page_cache>
          <backend>Cm_Cache_Backend_Redis</backend>
          <backend_options>
            <server>redis31.i</server>
            <port>6380</port>
            <persistent></persistent>
            <database>0</database>
            <password></password>
            <force_standalone>0</force_standalone>
            <connect_retries>3</connect_retries>
            <lifetimelimit>57600</lifetimelimit>
            <compress_data>0</compress_data>
          </backend_options>
        </full_page_cache>

Setting up garbage collection

We also encourage setting up the following garbage collection cron script. Create the file ./shell/redis.php in your Magento document root with the following content.

<?php count($argc) or die();
ini_set('memory_limit','1024M');
set_time_limit(0);
error_reporting(E_ALL | E_STRICT);
require_once '../app/Mage.php';
Mage::app()->getCache()->getBackend()->clean('old');
// uncomment this for Magento Enterprise Edition
// Enterprise_PageCache_Model_Cache::getCacheInstance()->getFrontend()->getBackend()->clean('old');

Then add the following cron entry to execute it every 4 hours

0 */4 * * * ( echo "cd /domains/example.com/http/shell/; php redis.php" | /usr/bin/fakechroot /usr/sbin/chroot /microcloud/domains/example /bin/bash )