A few days ago, I spoke about the architecture that powers Techie Buzz. In that post, I had mentioned that memcached was one of our key ingredients.
Memcache is a powerful tool to share objects across servers and it is used by several big sites including Facebook, Yahoo and many more. The best part about using memcached on your server is that you can cut down on your database queries significantly and store commonly used objects in memory and share it across multiple servers.
If you use WordPress on a multi-server setup, you can easily cut down on DB queries by up-to 50% by implementing memcached. If you are interested in knowing how to implement memcached on your server, here is a tutorial. The assumption is that you are using Ubuntu, if not, the installation steps might change.
How To Install Memcached And Configure It
Step 1: Install memcached using the command given below
sudo apt-get install memcached
Step 2: After you have installed memcached you will have to configure it through a .conf file, to do that use the command given below
Step 3: In the configuration file you will find few parameters, you can ignore the -d, -u and logfile parameters and leave it as default. However, change the -m, -p and -i parameters to suit your servers. The -m parameter specifies the memory that will be allocated to memcache. The -p parameter specifies the port that memcached will run on and the -l (ell) parameter specifies the IP address the memcached server will run on. You will need to change the -l (ell) parameter value from 127.0.0.1 to a local or global IP address of your server. This is necessary because your memcached server will be accessed by outside servers and using 127.0.0.1 will not work then.
Once you have done that run the following command.
service memcached restart or /etc/init.d/memcached restart
Using Memcached With WordPress
Once you have setup your memcached server, it is time to now setup WordPress to use memcached to store your objects. To do that, head over to http://wordpress.org/extend/plugins/memcached/ and install the memcached plugin for WordPress. Unlike other plugins, you will have to drop this file into the wp-content directory and not the plugins directory.
You will have to install additional plugins for your server to make use of this plugin, namely PECL memcache extension which can be downloaded from http://pecl.php.net/package/memcache.
Now comes the part where you will actually tell WordPress to use memcached as an object storing mechanism. To do that, open the wp-config file and add the line below to it.
$memcached_servers = array(‘default’ => array(‘memcache-server-ip-or-name:11211′));
The array is used to define multiple memcached servers which can be used for your site. Please make sure to edit the name/ip of the server and the port before you save the file.
That’s it. Now just refresh your cache and WordPress should start storing the objects in memcache and will access your database very sparsely. Do let me know if you have any problems or difficulties in setting this up through your comments.
Bonus Note: You can browse and manage your memcache server through a WordPress plugin called WP Memcached Manager which can be downloaded at http://wordpress.org/extend/plugins/wp-memcached-manager/.