Replace Magento’s File Cache Backend with an Ultrafast Memory Cache

One of the best hidden features of Magento is its ability to utilize different cache backends. By default, Magento stores its cache on the local file system which is not really optimized for high performance. Especially with slower SATA disks and even Solid-State Disks, this generates significant IO overhead. Our aim is to minimize latency so pages can load as quickly as possible. We will achieve this by moving the cache to RAM (random access memory), which provides virtually zero latency.

Cache backends

Magento uses a plugin architecture which allows us to easily replace the cache backend implementation. Premade implementations exist for the following cache backends:

  • File System (default)
  • Database
  • Memcached
  • Alternative PHP Cache (APC)
  • XCache
  • eAccelerator
  • Redis

We will now look at how to replace the file system cache with Memcached, a high-performance, distributed memory object caching system. Memcached is available in our Magento Professional and Enterprise hosting packages.

Two-level backend

While RAM is extremely fast, it provides much less storage capacity than durable storage technologies such as the file system or a database. Therefore, Magento supports a two-level backend, which makes it possible to combine two different cache backends – a fast but small one and a slower but bigger one. We recommend using Memcached as the fast backend and database or file system as the slow backend. Note: we are using very fast Solid-State Drives on all our servers which offers unprecedented performance compared to even the fastest SAS drives, so read/writes to the file system will actually be very fast compared to normal standards.

Configuration

Configuring the cache backends is as simple as editing the Magento configuration file (app/etc/local.xml).
<global>
... <cache>
<backend>memcached</backend> <slow_backend>database</slow_backend> <fast_backend>Memcached</fast_backend> <fast_backend_options> <servers> <server> <host>unix:///home/<username>/.memcached/memcached.sock</host> <port>0</port> <persistent>0</persistent> </server> </servers> </fast_backend_options> <memcached> <servers> <server> <host>unix:///home/<username>/.memcached/memcached.sock</host> <port>0</port> <persistent>0</persistent> </server>
</servers>
</memcached>
</cache>
... </global>

We run Memcached over Unix socket on our servers so you will need to add the correct path to the Memcached socket in the code above. Normally the path will be unix:///home/<username>/.memcached/memcached.sock, where <username> is your cPanel username. If Memcached is supported by your hosting plan just open a support ticket and we will enable it for your account.

That's all it is to it. Your Magento store is now configured to use Memcached as the fast backend. It will take a little while until the cache builds up before you notice the performance improvements, but then it should be much faster and able to handle more concurrent users.

If you have any questions feel free to contact our support department.