Using Redis with Magento

Redis is a modern key/value cache that supports a number of complex datatypes (lists, sets, hashes, …).

It runs via sockets so can be used for both networked and local operation.

  • For single webnode setups the connection should be configured via socket.
  • For multinode setups the connection should be configured via local private IP address (10.xx.xx.xx)

Redis is installed by Hosting to every dedicated server that is installed. Usually 2 separate instances of Redis are installed 

  • Redis for Backend cache (+ Full Page Cache if Enterprise Magento)
  • Redis for Sessions

Configuring redis-server for backend (and FullPageCache)

Install Redis Cache module to site.

    • We have a Magento module installable via ServerPortal: Icommerce_Redis

Configure Magento.

      • Redis-server for backend and fpc is running on port 6379, unless specified otherwise
      • <id_prefix> is used to avoid mixup up cache keys between different instances
      • <database> code can be the same for backend and fpc, but should be different between environments (staging vs live). Usually our Redis instances can hold up to 16 databases.
      • <compress_data> & <compress_tags> should NOT be enabled for FPC as there is compression already in FPC functionality
      • Edit app/etc/local.xml to configure:
      • local.xml cache config (UNIX socket)
  • <!– This is a child node of config/global –>
    <cache>
    <id_prefix>x8888</id_prefix> <!– Use instance code here, to avoid cache key mixups –>
    <backend>Cm_Cache_Backend_Redis</backend>
    <backend_options>
    <server>unix:///var/run/redis/redis-server.sock</server>
    <!– <server>10.xx.xx.xx</server>  TCP IP connection should be used if Redis is located in another server eg. in a multinode setup –>
    <port>6379</port>
    <persistent></persistent> 
    <database>0</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>
    </backend_options>
    </cache>

  • local.xml cache config for FPC (UNIX socket)

  • <!– This is a child node of config/global for Magento Enterprise FPC –>
    <full_page_cache>
    <id_prefix>x8888</id_prefix>
    <backend>Cm_Cache_Backend_Redis</backend>
    <backend_options>
    <server>unix:///var/run/redis/redis-server.sock</server>
    <!– <server>10.xx.xx.xx</server>  TCP IP connection should be used if Redis is located in another server eg. in a multinode setup –>
    <port>6379</port>
    <persistent></persistent>  
    <database>0</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>0</compress_data>
    <compress_tags>0</compress_tags>
    <lifetimelimit>57600</lifetimelimit>   
    <use_lua>0</use_lua>       
    </backend_options>
    </full_page_cache>
        • Regarding option automatic_cleaning_factor above, it is important that to set it to 0 (disable) since otherwise, Redis (or rather PhpRedis) will clean itself on every N:th request.

Configuring redis-server for sessions

        • By default the redis sessions is running on port 6380 (this is needed when )we run this extra redis-server-sessions on port 6380 (instead of redis’s default 6379, which we use for normal cache storage)

Configure Magento

        • Edit app/etc/local.xml to configure. For single-node site use socket based connection, for multinode environment use TCP based connection.
          local.xml sessions config
  • <!– This is a child node of config/global –>
    <session_save><![CDATA[redis]]></session_save>
    <session_save_path><![CDATA[unix:///var/run/redis/redis-server-sessions.sock?database=0]]></session_save_path>
    <!– <session_save_path><![CDATA[tcp://10.xx.xx.xx:6380?database=1]]></session_save_path>  TCP IP connection should be used if Redis is located in another server eg. in a multinode setup –>
     

Checking the Redis server status

          • To check Redis service statuses, run:
  • # For backend, FPC redis:
    $ service redis-server status
    # for Redis sessions:
    $ service redis-server-sessions status
          • Test if you can connect to local Redis vis socket by :
  • $ redis-cli
    # If you want to check the usage of redis, then type “info” in the redis-cli
    $ 127.0.0.1:6379> info
          • redis.conf: This can be used as a way to handle the case when cache memory is consumed:
  • maxmemory-policy allkeys-lru

  • .

Notes

Data Compression

Magento will pull significant amounts of configuration + layout data from the cache backend. Summing the size of all cache read requests, while building a page, on the TSW, indicated that around 1.7 MB was pulled from the cache. With Redis, this data has to travel over the network unless UNIX socket is used, and actually will dominate in size, over the final HTML generated (200-300 KB). We will conserve server bandwidth activating the setting to comress data in the PHP redis client. 

<compress_data>1</compress_data>

NOTE! We have observed intermittent errors of this type when configuring Redis without data compression on some higher traffic sites:

a:4:{i:0;s:24:”read error on connection”;i:1;s:1341:”#0 [internal function]: Credis_Client->__call(‘exec’, Array)

The error has completely gone away after activating compression in Redis. So compression is strongly recommended for several reasons.

Don’t use compression with FPC as it already includes sort of compression.

Links:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s