Today, I decided to implement a popular distributed caching system called memcached. Memcached allows PHP objects and variables to be stored in memory, which when used properly, can decrease page load time and server load. Memcached can also work as a central cache for a cluster of servers. File-based caching systems work by generating static files, and storing them in a local directory to be served in place of the dynamic content. This can reduce the number of database queries, and help avoid having to process the script with each new request; however, there is a downside.
Disk-based caching systems require information to be pulled from the hard drive, which is often the weakest link in a server. There have been many improvements in the storage arena, most notably solid-state drives that contain no moving parts, but most servers still use SAS drives. Storing information in memory can provide a much faster experience, and using memcached can help harness this power. Some people use file and disk-based caching interchangeably, but they can be slightly different. File-based caching systems can work with memory, whereas disk-based caching systems always pull from the hard drive.
The native WP_Object_Cache class
WordPress includes a basic caching mechanism, called WP_Object_Cache, that stays alive until a given page (script) has been processed, and sent to the browser via HTTP. WP_Object_Cache works more like a registry than a caching system, and most database-related functions have a system for utilizing this class. This can help save time and resources when data is used a multitude of times throughout execution.
When a user views any post, the get_post() function is called. Before querying the database for the post data, get_post() checks the WP_Object_Cache object to see if the data has been cached. If false is returned, the database query is completed, and the results are stored in WP_Object_Cache, if successful. As previously stated, after the page is sent, the information is then flushed from the cache.
Why memcached is sometimes better
Memcached runs as its own service on a server, and can be accessed via PHP by installing a module named MemCache, which is available via PECL. For the most part, PHP will destroy any information left after executing a script, including the native WP_Object_Cache object. This means that we have to start all over again when rendering another page, or creating a new request. The memcached process, on the other hand, is not destroyed when a PHP script is finished executing. We can store information there indefinitely, or until we are done with it; however, keep in mind that any information stored in memory is lost when the system is rebooted or shut down.
Memcached Object Cache for WordPress
After installing memcached, and activating the Memcached Object Cache plugin for WordPress, load time went down and the average number of database queries was reduced from 25 to 14. This will prove to be especially helpful as the site grows, as well as during times of high traffic. Like file, and disk-based caching, memcached also comes with a price. If memory is scarce, or information only needs to be communicated locally, a disk-based caching system may be better for your application. The key with this, like anything else in the world of development, is testing. Try both setups, and get feel for how each system works. You may find that your original setup was better, or you may be pleasantly surprised with the benefits of memcached.
I won’t go into details on how to install memcached on the server, but instead will only focus on the WordPress portion of the installation. At the bottom of this tutorial, I have included several great links on how to install memcached, and MemCache for PHP on Linux, as well as Windows. I have personally gone through these tutorials and they work fabulously.
The WordPress part of the installation is very simple. Follow the directions below:
- Download the Memcached Object Cache plugin for WordPress.
- Unzip object-cache.php.
- Move object-cache.php to /wp-content/object-cache.php.
- At this point, the Memcached Object Cache plugin has been installed. If you would like to be able to view the Memcached data inside of the WordPress admin panel, download and install the WP Memcached Manager plugin. This will also allow you to flush the data via the WP admin panel at any given time.
If you take a look at the new object-cache.php file, you will find that the functions are the same. WordPress checks for an object-cache.php within the wp-content directory (@see wp-settings.php, line 326 in WP 2.9), and if it exists, the native WP_Object_Cache class file is bypassed.
Further Reading and Downloads
- memcached.org (download latest version)
- PECL MemCache package for PHP (download latest version)
- Installing memcache on Windows for PHP
- Install MemCache on a server running cPanel (1)
- How to install memcached as a service on Linux (2)
- Distributed Caching with Memcached
Other WordPress-Memcached Plugins
Please feel free to post your experiences, and insight below. We would love to hear if memcached improved or decreased performance for your blog or application.