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.
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:
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
and replace with the following
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.
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.
You can execute the following from any node
echo "flush_all" | nc MEMCACHED_IP 11211
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.
Thanks for reading! I hope this has been useful to you, please do leave any feedback in the comments section below.