Caching WordPress with Batcache and Memcache
What memcache Does
memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load.
You can think of it as a short-term memory for your applications.
memcached allows you to take memory from parts of your system where you have more than you need and make it accessible to areas where you have less than you need.
memcached also allows you to make better use of your memory. If you consider the diagram to the right, you can see two deployment scenarios:
- Each node is completely independent (top).
- Each node can make use of memory from other nodes (bottom).
The first scenario illustrates the classic deployment strategy, however you'll find that it's both wasteful in the sense that the total cache size is a fraction of the actual capacity of your web farm, but also in the amount of effort required to keep the cache consistent across all of those nodes.
With memcached, you can see that all of the servers are looking into the same virtual pool of memory. This means that a given item is always stored and always retrieved from the same location in your entire web cluster.
Also, as the demand for your application grows to the point where you need to have more servers, it generally also grows in terms of the data that must be regularly accessed. A deployment strategy where these two aspects of your system scale together just makes sense.
The illustration to the right only shows two web servers for simplicity, but the property remains the same as the number increases. If you had fifty web servers, you'd still have a usable cache size of 64MB in the first example, but in the second, you'd have 3.2GB of usable cache.
Of course, you aren't required to use your web server's memory for cache. Many memcached users have dedicated machines that are built to only be memcached servers.
Setup WordPress Caching for Memcache
- Install memcached
- Install the PECL memcached extension.
- Install the Batcache Manager plugin.
- Install the Memcached Redux plugin.
Batcache Manager Plugin
Install the Batcache Manager plugin.
Batcache uses Memcached to store and serve rendered pages. It can also optionally cache redirects. It's not as fast as Donncha's WP-Super-Cache but it can be used where file-based caching is not practical or not desired. For instance, any site that is run on more than one server should use Batcache because it allows all servers to use the same storage.
Development testing showed a 40x reduction in page generation times: pages generated in 200ms were served from the cache in 5ms. Traffic simulations with Siege demonstrate that WordPress can handle up to twenty times more traffic with Batcache installed.
Batcache is aimed at preventing a flood of traffic from breaking your site. It does this by serving old pages to new users. This reduces the demand on the web server CPU and the database. It also means some people may see a page that is a few minutes old. However this only applies to people who have not interacted with your web site before. Once they have logged in or left a comment they will always get fresh pages.
Install the Memcached Redux plugin.
Memcached Object Cache provides a persistent backend for the WordPress object cache. A memcached server and the PECL memcache extension are required.
memcached PECL extension
Install the PECL memcached extension.
Memcached is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory. This extension allows you to work with memcached through handy OO and procedural interfaces.
CentOS / RedHat
Yum packages needed:
yum install libmemcached memcached memcached-devel php56u-pecl-memcache php56u-pecl-memcached
Pacman packages needed:
pacman -S memcached php-memcache libmemcached
Install phpMemcachedAdmin from its google code page.
Graphic stand-alone administration for memcached to monitor and debug purpose
This program allows to see in real-time (top-like) or from the start of the server, stats for get, set, delete, increment, decrement, evictions, reclaimed, cas command, as well as server stats (network, items, server version) with googlecharts and server internal configuration
You can go further to see each server slabs, occupation, memory wasted and items (key & value).
Another part can execute commands to any memcached server : get, set, delete, flush_all, as well as execute any commands (like stats) with telnet
To extract these informations, phpMemcacheAdmin uses, as you wish, direct communication with server, PECL Memcache or PECL Memcached API.
Memcache Tips and Tricks
Stay tuned for my next memcache article. It will consist entirely of practical uses or memcache and how to best utilize the power of this amazing tool for WordPress. Much of my initial learning and experience with this comes from my background working for WordPress VIP.