Using Redis with Magento 1.8+

Redis is memory-based key-value. Imagine Memcache with more tools such as clustering and multiple databases.

Magento can use Redis to:

  • Session
  • Cache
  • Full Page Cache (Enterprise)

What is the reason to use Redis?

  • It is faster and more efficient for the Magento backend. Recommended by Magento and most of Magento developers.
  • It can be shared between multiple balanced servers and replaces the use of Memcache for the backend cache.
  • Redis can also be used for as opposed to Memcache, so the cache is in memory. Read here the explanation of why this is good: http://www.fabrizio-branca.de/magento-zend-frameworks-twolevels-cache-backend-mess.html
  • Redis has persistence, whereas if you restart the memcache service you need to wait for a new cache.

And where should Redis be installed?

This will depend on the type of server you use, see examples:

  • Dedicated - Install Redis on the database server.
  • Dedicated Cluster - Redis can be configured as an RHCS service for HA.
  • Cloud - Install Redis on a server with plenty of free memory, usually the database. Another option is to use Redis-a-S. Li>
  • Single server - Still ideal to use if you have free memory. Use UNIX socket to remove TCP overhead. Li>

Installation and Configuration:

The first step is to check the version of your Magento, if it is CE 1.8+ or EE 1.13.1+ you can use Redis, if you do not use these versions you will need some module. Our example is only for versions EC 1.8+ and EE 1.13.1+.

The second step is to install and configure the Redis service. In our example we will install on the private IP database server 192.168.1.2. Install Redis by running the following commands:

yum install epel-release
yum install redis
chkconfig redis on

Configure o arquivo /etc/redis.conf, preste muita atenção que está é uma parte muito importante.

Set up the  /etc/redis.conf file, pay close attention that this is a very important part.

First change the bind to the private IP of your server, in our case:

bind 192.168.1.2

Let's change the configuration to use UNIX socket, for this uncomment the following lines:

unixsocket /tmp/redis.sock

unixsocketperm 755

In our case we only want memory cache, so we can disable disk persistence by commenting:

#save 900 1
#save 300 10
#save 60 10000

Something very important is that Redis was made for speed and not safety. Ideally, you should always use a private network or Cloud Networks. If you use a private network that can be seen by other users, let's set a password for Redis. It is recommended to create a large password because Redis speed facilitates a brute force. We will use this password:

FlfTlU6bAJQBVTtPFqJxX4xWamcjnFn28VBQhN3THN1fitMGlm

Still in the same file add the password:

requirepass FlfTlU6bAJQBVTtPFqJxX4xWamcjnFn28VBQhN3THN1fitMGlm

Still editing the same file we will change some maxmemory information:

maxmemory 1500mb

maxmemory-policy allkeys-lru

Save the file and start the Redis service by running the commando:

service iptables save

Finally open the 6379  port with the command:

iptables -A INPUT -p tcp -m state –state NEW -m tcp –dport 6379 -j ACCEPT
service iptables save

Now that we have the Redis service running in our database, we will install the PHP module on your web server in the third step strong>. You can easily deploy by running the command:

yum install php-pecl-redis.x86_64 -y

After installation restart Apache/nginx.

The fourth and final step is the Magento setup. Open the app/etc/local.xml file and place the following information between the tag: <gloal>:

  • Sessões (https://github.com/colinmollenhour/Cm_RedisSession/blob/master/README.md):
            <session_save>db</session_save>
            <redis_session>
                <host>192.168.1.2</host>
                <port>6379</port>
                <password></password>
                <timeout>2.5</timeout>
                <persistent></persistent>
                <db>0</db>
                <compression_threshold>2048</compression_threshold>
                <compression_lib>gzip</compression_lib>
                <log_level>1</log_level>
                <max_concurrency>6</max_concurrency>
                <break_after_frontend>5</break_after_frontend>
                <fail_after>10</fail_after>
                <break_after_adminhtml>30</break_after_adminhtml>
                <first_lifetime>600</first_lifetime>
                <bot_first_lifetime>60</bot_first_lifetime>
                <bot_lifetime>7200</bot_lifetime>
                <disable_locking>0</disable_locking>
                <min_lifetime>60</min_lifetime>
                <max_lifetime>2592000</max_lifetime>
                <log_exceptions>0</log_exceptions>
            </redis_session>

 

  • Cache (https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/blob/master/README.md):
    <cache>
      <backend>Cm_Cache_Backend_Redis</backend>
      <backend_options>
        <server>192.168.1.2</server>
        <port>6379</port>
        <persistent></persistent>
        <database>1</database>
        <password></password>
        <force_standalone>0</force_standalone>
        <connect_retries>1</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>gzip</compression_lib>
        <use_lua>0</use_lua>
        <load_from_slave>tcp://redis-slave:6379</load_from_slave>
      </backend_options>
    </cache>

 

  • Full Page Cache (Enterprise somente) (https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/blob/master/README.md):
    <full_page_cache>
      <backend>Cm_Cache_Backend_Redis</backend>
      <backend_options>
        <server>192.168.1.2</server>
        <port>6379</port>
        <persistent></persistent>
        <database>3</database>
        <password></password>
        <force_standalone>0</force_standalone>
        <connect_retries>1</connect_retries>
        <lifetimelimit>57600</lifetimelimit>
        <compress_data>0</compress_data>
        <auto_expire_lifetime></auto_expire_lifetime>
        <auto_expire_refresh_on_load></auto_expire_refresh_on_load>
      </backend_options>
    </full_page_cache>

 

Now that we have configured the local.xml, we will activate the Redis module for Magento, so open app/etc/modules/Cm_RedisSession.xml file and change false to true.