Magento Cache strategy for best performance


There are many blog posts about magento cache and how to configure it right.
In this blog post i want to explain which cache backend (File System, APCMemcachedRedis) is recommended in which environment (single server, multi server) and how to configure it.


Understanding Magento’s Two-Level Caching

Magento uses by default the two level cache backend from zend framework.
Basically it’s about storing cache records in two backends, in a very fast one (but limited) like APC
or Memcached and in a “slow” one like file system.
Each cache backend has it’s features. APC and Memcached are key/value cache backends, they don’t support tagging (grouping of cache entries). File system and Redis has tagging support.

Here you see how two level caching i supposed to work: (Thanks to Fabrizio Branca)

Magento Two Level Cache

Explanation of magento cache backends

File system (var/cache)

By default, Magento stores it’s cache entries in the file system, you find the cache in var/cache/.
This cache backend is fine for small websites with a small cache size but if you get more and more requests through
visitors, reading and writing to the file system gets slower and slower from time to time because the cache grows.
The Magento cache is organized by tags, this means you have cache entries which belongs to a cache group.
It works by default, you don’t need to install additional software.
Magento clears cache entries by tag e.g. after placing an order or saving a product to make sure the block caches are updated on the store. During clearing entries by cache tag magento has to open each file to check if the cache entry belongs to the cache group. If you have a magento shop with >= 1000 products you will have a cache size of > 50 MB. A 50 MB file system cache has about 3500 entries (files), you can imagine that this is very slow.
Tips for better file system performance
1. Use a SSD instead of normal hard disk
2. Put the var/cache directory in tmpfs

APC – Alternative PHP Cache (Key/Value)

APC stands for Alternative PHP Cache and is a free and open opcode cache for PHP. It’s goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
A very fast cache backend.
With APC on your server you can automatically have duplicate PHP script executions optimized to run more efficiently.
APC also provides a user cache for storing application data.
APC does not support tagging, you still have a slow file system as slow level cache.
PHP APC extension must be installed on server.
Tips for better performance
Make sure to give APC enough memory by the parameter “apc.shm_size”
Checkout our best practise configuration for php.ini (below)

Configuration (app/etc/local.xml)
Settings for php.ini
apc.enabled = 1
apc.optimization  = 0
apc.shm_segments = 1
apc.shm_size = 768M
apc.ttl = 48000
apc.user_ttl  = 48000
apc.num_files_hint = 8096
apc.user_entries_hint = 8096
apc.mmap_file_mask = /tmp/apc.XXXXXX
apc.enable_cli = 1
apc.cache_by_default  = 1
apc.max_file_size = 10M
apc.include_once_override = 0

Memcached (Key/Value)

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
A very fast cache backend.
Memcached does not support tagging, you still have a slow file system as slow level cache.
– Memcached server
– PHP extension for memcached

Configuration (app/etc/local.xml)
  <backend>memcached</backend><!-- apc / memcached / empty=file -->
  <memcached><!-- memcached cache backend related config -->
    <servers><!-- any number of server nodes can be included -->

Redis – Advanced key-value store with full cache tag support

This magento cache backend allows you to use a redis server as a central storage. Cache tags
are supported, we don’t need to use the slow level file system cache anymore.
This magento cache backend is highly recommended in multi server environments where you have more than one webserver.
Very fast cache backend with fully cache tag support, no slow level file system cache is needed
Tested on high traffic magento stores with more than 500.000 visitors / day, performance is great and stable.
– Redis must be installed on the server
– PHP Extension phpredis must be installed
– Magento extension “Cm_Cache_Backend_Redis” must be installed


1. Install redis (2.4+ required)
2. Install phpredis
3. Install the magento extension “Cm_Cache_Backend_Redis”
4. Edit your app/etc/local.xml

    <server></server> <!-- or absolute path to unix socket -->
    <compression_lib>gzip</compression_lib> <!-- Supports gzip, lzf and snappy -->
Useful tool for redis


phpRedisAdmin is a simple web interface to manage Redis databases.




If you have a small store with a small cache size then APC + file system as second level
cache would be fine for you. Furthermore i recommend to use a SSD and to put the cache directory into tmpfs.
If you have a bigger store and a dedicated server you should take a look a redis, it’s very fast also for big cache sizes > 500 MB. Redis is also the perfect cache solution in a multi server environment where you need a central cache.



, ,