Home » Magento tutorial for developer » How to configure Magento Cache for best performance

How to configure Magento Cache for best performance

How to configure Magento Cache for best performance?

Magento Cache enhances your site’ performance, provide a faster load speed, so how to configure it properly for best performance?

In this tutorial, we will find out which backend cache (File System, APCMemcachedRedis) is recommended to use in which environment (single server ormultiple server) and how to configure it the rigth way.

Understanding Magento’s Two-Level Caching

By default, Magento uses two-level cache backend from zend framework. To be simple, it stores 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:

 magento two level caching overview

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.
Advantages
It works by default, you don’t need to install additional software.
Disadvantages:
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.
Advantages
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.
Disadvantages
APC does not support tagging, you still have a slow file system as slow level cache.
Requirements
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

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.
Advantages
A very fast cache backend.
Disadvantages
Memcached does not support tagging, you still have a slow file system as slow level cache.
Requirements
- Memcached server
- PHP extension for memcached

Configuration (app/etc/local.xml)

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.
Advantages
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.
Requirements:
- Redis must be installed on the server
- PHP Extension phpredis must be installed
- Magento extension “Cm_Cache_Backend_Redis” must be installed

Installation

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

Useful tool for redis

phpRedisAdmin

phpRedisAdmin is a simple web interface to manage Redis databases.
Demo: http://dubbelboer.com/phpRedisAdmin/?overview

phpredisadmin

Conclusion

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.

About Brian Wilson

avatar
Brian is a freelance Magento developer with 5 years of experience with customizing Magento extensions, magento themes. He's interested in sharing Magento Tutorials to the community

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top