Magento Expert Forum - Improve your Magento experience
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, APC, Memcached, Redis) 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)
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)
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 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
<!-- apc / memcached / empty=file --> <memcached>
<!-- memcached cache backend related config -->
<!-- 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
<!-- or absolute path to unix socket -->
<!-- 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.
View more threads in the same category:
The Following User Says Thank You to david For This Useful Post:
WooooooooooW!!! Very technical and really helpful! Thanks for sharing.
Last edited by Parth Patel; 02-08-2013 at 06:56 AM.
:: Magento eCommerce Design Agency
Yes! agreed one of the best Platform of Ecommerce. Magento plays a vital role for E-Business Companies. This reason is good, but some issues like speed, cache and multisite are suffer mostly magento users. CloudWays bring a best service to Managed Complete platform to support and solve technical issues of Magento.
Managed Magento Hosting
That was a great post and the coding was useful too!!!
By david in forum Magento Speed up, Performance and Optimize
Last Post: 13-06-2016, 12:19 PM
By brent in forum Magento Speed up, Performance and Optimize
Last Post: 26-02-2015, 02:48 PM
By shunavi in forum Magento Speed up, Performance and Optimize
Last Post: 14-10-2014, 11:14 PM
By sgdev in forum Magento Speed up, Performance and Optimize
Last Post: 29-05-2014, 10:47 AM
By david in forum Programming & Development
Last Post: 24-03-2013, 09:33 AM
Tags for this Thread