Friday, February 27, 2015

Configuring Magento To Use Redis

Enable the Cm_RedisSession module:

# vim app/etc/modules/Cm_RedisSession.xml

<active>true</active>

Copy the necessary codes from app/etc/local.xml.additional to app/etc/local.xml:

Put the following code inside <global>   </global>.

# vim app/etc/local.xml

<!-- example of redis cache -->
        <cache>
            <backend>Mage_Cache_Backend_Redis</backend>
            <backend_options>
                <server>127.0.0.1</server> <!-- or absolute path to unix socket for better performance -->
                <port>6379</port>
                <database>0</database>
                <password></password>
                <force_standalone>0</force_standalone>  <!-- 0 for phpredis, 1 for standalone PHP -->
                <connect_retries>1</connect_retries>    <!-- Reduces errors due to random connection failures -->
                <automatic_cleaning_factor>0</automatic_cleaning_factor> <!-- Disabled by default -->
                <compress_data>1</compress_data>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
                <compress_tags>1</compress_tags>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
                <compress_threshold>20480</compress_threshold>  <!-- Strings below this size will not be compressed -->
                <compression_lib>gzip</compression_lib> <!-- Supports gzip, lzf and snappy -->
                <persistent>1</persistent> <!-- persistence value, 0: not in use, > 0 used as persistence ID -->
            </backend_options>
        </cache>

        <!-- example of redis full page cache -->
        <full_page_cache>
            <backend>Mage_Cache_Backend_Redis</backend>
            <backend_options>
                <server>127.0.0.1</server> <!-- or absolute path to unix socket for better performance -->
                <port>6379</port>
                <database>1</database>
                <password></password>
                <force_standalone>0</force_standalone>  <!-- 0 for phpredis, 1 for standalone PHP -->
                <connect_retries>1</connect_retries>    <!-- Reduces errors due to random connection failures -->
                <automatic_cleaning_factor>0</automatic_cleaning_factor> <!-- Disabled by default -->
                <!-- in FPC data is already gzipped, no need to do this twice -->
                <compress_data>0</compress_data>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
                <compress_tags>1</compress_tags>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
                <compress_threshold>20480</compress_threshold>  <!-- Strings below this size will not be compressed -->
                <compression_lib>gzip</compression_lib> <!-- Supports gzip, lzf and snappy -->
                <lifetimelimit>43200</lifetimelimit> <!-- set lifetime for keys without TTL -->
                <persistent>2</persistent>
            </backend_options>
        </full_page_cache>

        <!-- example of redis session storage -->
        <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 Redis server requires authentication -->
            <timeout>2.5</timeout>            <!-- This is the Redis connection timeout, not the locking timeout -->
            <persistent></persistent>         <!-- Specify unique string to enable persistent connections. E.g.: sess-db0; bugs with phpredis and php-fpm are known: https://github.com/nicolasff/phpredis/issues/70 -->
            <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 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 -->
            <break_after_adminhtml>30</break_after_adminhtml>
            <bot_lifetime>7200</bot_lifetime>                    <!-- Bots get shorter session lifetimes. 0 to disable -->
        </redis_session>

Redis support across different Magento versions

Magento CE >= 1.7.0.0 and < 1.8.0.0
  • Session storage – not included
  • Cache backend – not included, after installation available as Cm_Cache_Backend_Redis

Magento CE >= 1.8.0.0
  • Session storage – included
  • Cache backend – included, available as Mage_Cache_Backend_Redis

Note: make sure you do change:

From:

<backend>Cm_Cache_Backend_Redis</backend>

To:

<backend>Mage_Cache_Backend_Redis</backend>

Note: make sure you do change the value of the following tag inside <redis_session> tag:

From:

<db>0</db>

To:

<db>2</db>

# semanage boolean -l | grep httpd_can_network

To allow Apache to connect to the Redis server by enabling all ports:

# setsebool -P httpd_can_network_connect 1

Note: If you have turned on Security-Enhanced Linux (SELinux), httpd scripts by default are not allowed to connect out to the network.

After enabling Redis as cache backend, var/cache directory of your Magento installation can be emptied and should stay empty:

# rm -rf /var/www/html/magento19/var/cache

# ls /var/www/html/magento19/var/cache

Or Log in to the Admin Panel as an administrator. Click System > Cache Management > click Flush Magento Cache at the top of the page.

Run the redis command line tool:

# redis-cli

127.0.0.1:6379> info keyspace
127.0.0.1:6379> select 0
127.0.0.1:6379> keys *
127.0.0.1:6379> flushdb
127.0.0.1:6379> keys *

Some of the commands you’ll be using most of the time are definitely:

  • FLUSHALL – clear all databases
  • SELECT # – select database under index #
  • FLUSHDB – empty currently selected database
  • KEYS * – list all keys from currently selected

Reference:

http://inchoo.net/magento/using-redis-cache-backend-and-session-storage-in-magento/
http://www.magentocommerce.com/knowledge-base/entry/redis-magento-ce-ee

No comments: