NOTE: This post explains setting up memcache on Drupal 6. We will very soon follow up for Drupal 7.
Why do I need memcache for my drupal site?
Drupal's default cache mechanism stores a lot of cached content in the database, so every time an http request needs this cached content, they have to make a request to the cache tables of the drupal database. This means the web server has to load your database server into the memory ...NOTE: This post explains setting up memcache on Drupal 6. We will very soon follow up for Drupal 7.
Why do I need memcache for my drupal site?
Drupal's default cache mechanism stores a lot of cached content in the database, so every time an http request needs this cached content, they have to make a request to the cache tables of the drupal database. This means the web server has to load your database server into the memory. So lets say we had to load the entire page (from cache_page) there is no way of skipping calls to the database. Memcache 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. In our case, memcache stores the cached entries in the system RAM and updates itself whenever the corresponding cache tables are updated. To sum up, you save a lot of computing resources when you start using memcache. And yes, the big guns are using it too :-) Youtube, Facebook, Twitter, Wikipedia, Flickr et al use this service. Read the google's faq to understand how memcache works - http://code.google.com/p/memcached/wiki/FAQ#How_does_memcached_work?
Now that you want to setup memcache on your drupal site, lets jump into the installation. [Note: the below mentioned installation is for ubuntu server and not any other operating system.]
- Install memcached on your server: sudo aptitude install memcached (avoid sudo when already logged in as root)
- Install the php5-dev package if not already done: sudo aptitude install php5-dev (you'll need the dev package to phpize the pecl memcache library)
- Install the PECL memcache library:
- cd /usr/local/src
- sudo wget http://pecl.php.net/get/memcache-3.0.6.tgz (look at http://pecl.php.net/package/memcache to find the latest version available)
- sudo tar -xzvf memcache-3.0.6.tgz
- sudo cd memcache-3.0.6
- sudo phpize
- sudo ./configure
- sudo make
- sudo make install
- Make sure your php loads the new memcache library: append the following lines in your php.ini (the one that is used by your web server, e.g. if you are using apache, your configuration file is likely to be at /etc/php5/apache2/php.ini)
extension = memcache.so
memcache.hash_strategy="consistent"
read more about consistent hashing technique at http://code.google.com/p/memcached/wiki/FAQ#What_is_a_"consistent_hashing"_client? - Restart your web server: sudo /etc/init.d/apache2 restart
- Okay thats a lot of commands, lets now see if we are moving in the right track. Create a info.php file under your root document and just have the following php code in it Now, call this file from your browser, e.g. [servername]/info.php and if you see something like the following then you are good:
memcache
memcache support enabled
Version 3.0.6
Revision $Revision: 310129 $
Else, post your issue as a comment and I'll try to help - Initiate Memcache instances: What cache tables do you want to use with memcache? You understand that only when you take a look at your database. What cache tables have decent number of entries? Those are the ones you would want to store in your RAM. Lets just assume you want to go for all the cache tables and lets also assume that there are 8 cache tables namely cache, cache_block, cache_content, cache_filter, cache_form, cache_menu, cache_page, cache_views. So here is what you do:
sudo memcached -u www-data -p 11211 -m 2 -d
sudo memcached -u www-data -p 11212 -m 2 -d
sudo memcached -u www-data -p 11213 -m 2 -d
sudo memcached -u www-data -p 11214 -m 2 -d
sudo memcached -u www-data -p 11215 -m 2 -d
sudo memcached -u www-data -p 11216 -m 2 -d
sudo memcached -u www-data -p 11217 -m 2 -d
sudo memcached -u www-data -p 11218 -m 2 -dNow that would create 8 daemons of memcache instances and you can see the processes with the command ps -A | grep memcached
But thats not it, you need to make sure these instances are running everytime you restart your server. Lets just add the above 8 lines in the init command of memcache located at /etc/init.d/memcached -- add it in the "start" if clause. And now try to restart memcache sudo /etc/init.d/memcached restart and then make sure you test whether the instances are initiated using the same ps command that we tried a short while ago. - Install memcache module in your drupal code base.
[Advice: Start using drush, it makes life easier, you just need to do a drush dl memcache]
[Note: Do not enable the module yet] - PUT YOUR SITE TO OFFLINE MODE before continuing else you'll run into the risk of white screen of death
- Modify your settings.php (located in sites/default) so that it knows the relation between the cache tables and the memcache instances. Append the following line in settings.php:
$conf = array( 'cache_inc' => './sites/all/modules/contrib/memcache/memcache.db.inc', 'memcache_servers' => array( 'localhost:11211' => 'default', 'localhost:11212' => 'filter', 'localhost:11213' => 'menu', 'localhost:11214' => 'page', 'localhost:11215' => 'form', 'localhost:11216' => 'content', 'localhost:11217' => 'views', ), 'memcache_bins' => array( 'cache' => 'default', 'cache_filter' => 'filter', 'cache_menu' => 'menu', 'cache_page' => 'page', 'cache_form' => 'form', 'cache_content' => 'content', 'cache_views' => 'views', ), ); - Enable the memcache module
- You can now put your site to ONLINE mode now
- Besides an improved performance, you can also check whether your memcache is responding via the urls admin/settings/memcache and admin/reports/memcache
Do post your comment, even if this post was helpful :-)


Comments
File name should be
File name should be memcache.db.inc or memcache.inc ?
'cache_inc' => './sites/all/modules/contrib/memcache/memcache.db.inc'
OR
'cache_inc' => './sites/all/modules/contrib/memcache/memcache.inc'
If there are large custom tables which are only accessed by the web-services, should it be normally included as above mentioned or there are some specific instructions?
Thanks,
Prateek
Prateek, it should be
Prateek, it should be
'cache_inc' => './sites/all/modules/contrib/memcache/memcache.db.inc'
Large tables will also be included normally, but I dont think having those tables in the memcache would help because unless the data is in BLOB format, it takes a lot of space in structuring the data in the database and therefore in the system cache when handled with memcache. My suggestion would be to use keys in the large table based on the requirement.
Hi, first thanks for your
Hi,
first thanks for your explanations !
I don't know where you have found the file memcache.db.inc, it's not in the module (even the RC2 or the dev release... ?) but even by replacing memcache.db.inc by memcache.inc I could not get it working...
here is my working settings.php, in conjunction with apc
Hi Tom, My bad, I should
Hi Tom,
My bad, I should have mentioned that the above steps are for Drupal 6. I'll edit the post and make those modifications straight away.
There could be multiple reasons for your memcache not to work. The most basic reason could be your php configuration, make sure you have enabled memcache:
extension = memcache.so
memcache.hash_strategy="consistent"
then to verify whether your memcache is working, you could do a sanity check using some php code. Create a test.php in your document root and write the following code there and then run it:
Let me know if this was helpful.
Mukesh
Thanks a lot for the
Thanks a lot for the explanation on how to use memcache !
it already but I'm so glad I
it already but I'm so glad I found this site Keep up the good work I read a lot of blogs on a daily basis and for the most part, plepoe lack substance but, I just wanted to make a quick comment to say GREAT blog. Thanks, :)A definite great read..Jim Bean
Add new comment