install and integrate memcached in drupal

Hi Every one,
If performance is the issue for your Drupal site then one thing you are missing is memcache. This article explains you step by step process to install and memcache and how memcache helps to boost your drupal website.

What is memcache ?
Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached's magic lies in its two-stage hash approach. It behaves as though it were a giant hash table, looking up key = value pairs. Give it a key, and set or get some arbitrary data.

When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data.

For example, if we have clients 1, 2, 3, and servers A, B, C:

Client 1 wants to set key "foo" with value "barbaz". Client 1 takes the full list of servers (A, B, C), hashes the key against them, then lets say ends up picking server B. Client 1 then directly connects to server B, and sets key "foo" with value "barbaz". Next, client 2 wants to get key "foo". Client 2 runs the same client library as client 1, and has the same server list (A, B, C). It is able to use the same hashing process to figure out key "foo" is on server B. It then directly requests key "foo" and gets back "barbaz".

Installing Memcache on Linux Machine :

Step 1: Run this command
~$> sudo aptitude install memcached
Step 2: Run this command
~$> sudo aptitude install php5-dev
Step 3: Installing PECL memcache

~$> cd /usr/local/src
~$> wget http://pecl.php.net/get/memcache-2.2.5.tgz
~$> tar zxvf memcache-2.2.5.tgz
~$> cd memcache-2.1.2
~$> phpize
~$> ./configure
~$># make && make install
Step 4: Open your php.ini file at /etc/php5/apache2/php.ini and add the below line
extension = memcache.so
Step 5: Restart Apache ~$>sudo /etc/init.d/apache2 restart
Step 6: If your memcache is installed properly you can see memcache configurations at http://localhost/info.php
Basically info.php is a file at /var/www folder with code <?php echo phpinfo(); ?>
Step7: The optimal number of daemons depends on your needs, but generally you want one for each cache table in your Drupal database because this makes clearing the cache on any of those tables less disruptive to the rest of your cache. So try to add these lines in memcached file in /etc/init.d/

memcached -u www-data -p 11211 -m 2 -d
memcached -u www-data -p 11212 -m 2 -d
memcached -u www-data -p 11213 -m 2 -d
memcached -u www-data -p 11214 -m 2 -d
memcached -u www-data -p 11215 -m 2 -d
memcached -u www-data -p 11216 -m 2 -d
Step 8: Restart memcache and try this command to verify the memcached daemons that are running.

~$>ps -A | grep memcached
23846 ?        00:00:00 memcached
23848 ?        00:00:00 memcached
23850 ?        00:00:00 memcached
23852 ?        00:00:00 memcached
23854 ?        00:00:00 memcached
23856 ?        00:00:00 memcached
Now memcache is installed and running on your system. Its time to integrate with drupal now.
Step 9: Download the drupal memcache module from here http://drupal.org/project/memcache .
Step 10: Open your php.ini file at /etc/php5/apache2/php.ini and add the below line
memcache.hash_strategy="consistent"
Step 11: Put your site offline.
Step 12: Edit your settings.php file in drupal /sites/default directory with below code.

$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' => 'block',
),
'memcache_bins' => array(
  'cache' => 'default',
  'cache_filter' => 'filter',
  'cache_menu' => 'menu',
  'cache_page' => 'page',
  'cache_form' => 'form',
  'cache_block' => 'block',
  ),
);
Step 13: Enable the memcache module that you downloaded at step 9.
Step 14: Now if you enable memcache statistics at http://project_folder/admin/settings/memcache then you can see the statistics something like below if your memcache is properly installed and running.

* get:
 variables
   theme_registry:garland
   links:navigation:page-cid:admin/settings/memcache:1
   links:navigation:tree-data:3c28d675de5c9165717c8bc9be1c436c
   links:primary-links:page-cid:admin/settings/memcache:1
   links:primary-links:tree-data:5d6d3aaaaef5fba302ce62698fa37bbe
   links:secondary-links:page-cid:admin/settings/memcache:1
   links:secondary-links:tree-data:5d6d3aaaaef5fba302ce62698fa37bbe
* set:
  variables
    theme_registry:garland
* hit:
    links:navigation:page-cid:admin/settings/memcache:1
    links:navigation:tree-data:3c28d675de5c9165717c8bc9be1c436c
    links:primary-links:page-cid:admin/settings/memcache:1
    links:primary-links:tree-data:5d6d3aaaaef5fba302ce62698fa37bbe
    links:secondary-links:page-cid:admin/settings/memcache:1
    links:secondary-links:tree-data:5d6d3aaaaef5fba302ce62698fa37bbe
* bins:
    cache
    cache
    cache
    cache
    cache_menu
    cache_menu
    cache_menu
    cache_menu
    cache_menu
    cache_menu
Thats it you are done and installed memcache.!!
Cheers,



Comments

Popular posts from this blog

ubuntu package installation

Drupal Bootstrap Database

How to fix 500 internal privoxy error