In the default configuration, Magento use the file system to store the cache. This is slow especially with traditional SATA disks, but even if faster Solid State Disks are used, great performance improvements can be achieved by replacing the file system with a faster cache backend. In this tutorial we will show you how to use Memcached with Magento, a memory based object cache that removes IO latency and speed up page loads significantly.
Magento has a flexible plug-in architecture which makes it really simple to replace the cache implementation. The cache uses a two-level design, a fast backend which stores the most frequently accessed data, and a slower (but bigger) cache used to store data that does not fit in the fast backend. We will use Memcached for the fast backend and the database as the slower backend to get the benefits of both speed and storage capacity.
To enable the Memcached backend in Magento, edit the app/etc/local.xml as following:
Code:
<global>
...
<cache>
<backend>memcached</backend>
<slow_backend>database</slow_backend>
<fast_backend>Memcached</fast_backend>
<fast_backend_options>
<servers>
<server>
<host>Memcached address or socket</host>
<port>0</port>
<persistent>0</persistent>
</server>
</servers>
</fast_backend_options>
<memcached>
<servers>
<server>
<host>Memcached address or socket</host>
<port>0</port>
<persistent>0</persistent>
</server>
</servers>
</memcached>
</cache>
...
</global>
In the host setting above you need to type in the address where your Memcached server is running. It also accepts a unix socket in the following format: unix:///path/to/memcached.sock.
Magento is now configured to use Memcached and you should immediately notice a performance improvement! To verify that Memcached is working, you can telnet to the Memcached server or use the nc command through SSH to connect directly to the socket. Then type "stats".
Code:
# nc -U /path/to/memcached.sock
stats
STAT pid 608573
STAT uptime 604274
STAT time 1386238869
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 3.598452
STAT rusage_system 21.105791
STAT curr_connections 1
STAT total_connections 1363
STAT connection_structures 18
STAT cmd_get 187986
STAT cmd_set 4559
STAT cmd_flush 0
STAT get_hits 157590
STAT get_misses 30396
STAT delete_misses 21
STAT delete_hits 13
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 36698235
STAT bytes_written 5522895521
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 17152993
STAT curr_items 716
STAT total_items 4559
STAT evictions 0
END
Here are some interesting numbers:
Code:
STAT total_connections 1363
STAT get_hits 157590
STAT get_misses 30396
STAT bytes 17152993
STAT evictions 0
get_hits should be considerably higher than get_misses. If evictions is > 0 it means cache entries were removed to free space for new items. If you are seeing a high eviction rate, you should increase the memory available to Memcached.
This tutorial is based on this blog post.
View more threads in the same category:
Bookmarks