Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

How to Configure Magento with Memcached and Redis

So here is my promised follow up post on how to configure Magento with Memcached and Redis. For those of you that missed my last post, this article follows on from one I posted a couple of weeks ago on Installing Magento with nginx and Zend Server in a production environment which you can read at our blog

So let’s get straight into it!


 

1. Memcached and Redis

Firstly, install Memcached and Redis, preferably on a server which isn't your web server. I would suggest your database server would be the best to use as its memory isn't fully utilised.

An important configuration point to note is to NOT allow persistent storage on your Redis installation as disk I/O is slow. In addition, you should allocate 8GB of memory to Redis and 4GB to Memcached.

 

2. PhpRedis

Firstly install the phpRedis extension onto your web nodes. This is very important; initially I didn’t do this and it caused my Redis cache not to share session information properly between the nodes.

If you are running Zend Server I found the easiest way to install phpredis is through pecl. To do this browse to your zend bin folder; usually found at /usr/local/zend/bin, then execute pecl install redis 

Should you receive any installation errors this is likely to mean you would need to install autoconf and make. You can do this either through apt-get install, make, autoconf or yum install make autoconf, depending on your architecture. Additionally you may wish to temporarily ensure that your /tmp folder is executable. To do this run mount -o remount,exec /tmp and after the install mount -o remount,noexec /tmp

 

3. Memcache and Memcached modules

Firstly if you are using Zend server remember to enable the memcache and memcached modules. These can be found in:

/usr/local/zend/etc/conf.d/memcache.ini

and 

/usr/local/zend/etc/conf.d/memcached.ini 

 

4. Ensure everything is working

Restart your web server to ensure all changes that were made take effect. To view whether the new modules have loaded correctly or not you can run /usr/local/zend/bin/php -i | grep 'module' where module is the module you would like to check.

 

5. Magento configuration

The final step of this process is to configure your magento setup to use the caching systems now available to us.

So firstly, in your local.xml on every web node do the following:

(This is assuming you have the normal layout of local.xml)

Delete the entry for

<session_save><![CDATA[files]></session_save>]

and replace with the following

<session_save><![CDATA[memcache]]></session_save><session_save_path><![CDATA[tcp://MEMCACHED_IPADDRESS:11211?persistent=1&weight=2&timeout=10&retry_interval=10]]></session_save_path><cache><backend>Mage_Cache_Backend_Redis</backend><backend_options><server>REDIS_IP_ADDRESS</server><port>6379</port><persistent></persistent><database>0</database><password></password><force_standalone>1</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></backend_options></cache>

If you are using Enterprise edition you can add the following section directly below the previous one or in the correct node in enterprise.xml.

Please note that the cache and full page cache use different database numbers.

<full_page_cache><backend>Mage_Cache_Backend_Redis</backend><backend_options><server>REDIS_IPADDRESS</server> <port>6379</port><persistent></persistent><database>1</database><password></password><force_standalone>1</force_standalone><connect_retries>1</connect_retries><lifetimelimit>57600</lifetimelimit><compress_data>0</compress_data></backend_options></full_page_cache>

 

And that's it! You can now remove the contents of your var folder and everything should be running fine.

 

6. Flushing the caches

If you want to flush the cache there are a few methods that you can use. The method I am about to explain is the one I found the easiest and simplest after trying a few options.

Please note that you don't want to flush your Memcahed cache in your live environment as this will end your customer's session on your site. It is recommended to rather use the Magento Cache Management page to do this.

Memcached

You can execute the following from any node

echo "flush_all" | nc MEMCACHED_IP 11211

Redis

On the server where Redis is installed run  redis-cli -h REDIS_IP

To flush everything run flushall. To flush only one database selectdb 0 (where 0 is the db you want to flush) and then flushdb.

 

7. Useful links

Here are some useful links that I came across whilst researching this post.

https://github.com/nexcess/whitepaper-v1-configs

https://gist.github.com/parhamr/6177160

http://www.mgt-commerce.com/blog/the-right-magento-cache-strategy-for-best-performance/

https://github.com/colinmollenhour/Cm_Cache_Backend_Redis

 

Thanks for reading! I hope this has been useful to you, please do leave any feedback in the comments section below.