Wednesday, February 15, 2012

Memcached Overview

 General

What is memcached:  "Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load." You may want to visit: Memcahed Wiki.

Memcached is developed in C programming language so that makes it cross platform and many other languages  have API interfaces to it so it can be used with lot of other technologies.

Installation

Installation on linux is a breeze :)  just run this command in terminal

- sudo apt-get install memcached

I got to a problem when installing memcached with a missing library pcre.h 
Well run this command in terminal

- sudo apt-get install libpcre3-dev


And I found this blog with the answer for this at pcre.h missing
Memcached has some default settings which are best for most server configurations so select all defaults when installing. If you have some special requirements visit the conf page .

If you want to use Memcached with PHP you will need PECL extension called memcache which is not included by default with PHP package, so you have install it manually. Open terminal and run this command

- sudo pecl install memcache

Accept all defaults. Once it is built and installed you may need to add this line to your php.ini file

- extension=memcache.so

into you extension section.

Well thats it for the installation, you are ready for caching :)

 Tutorial

When starting up with Memcached I recomend reading of the Caching story :) and also to illustraded one :)

So the basic idea of memcached is to store all your server adresses in an array like this

$MEMCACHE_SERVERS = array(
   "10.1.1.1", //web1
   "10.1.1.2", //web2
   "10.1.1.3", //web3
);

and put them into a memcache object like this

$memcache = new Memcache();
    foreach($MEMCACHE_SERVERS as $server){
    $memcache->addServer ( $server );
}


The next thing you should do is to use memcached i.e utilize the caching systems on all your servers in the cluster. In this example we have 3 with one 1 GB RAM, so that means 3 GB of distributed cache.

 Illustrations

Below are some images I made about memcahed usage and structure







No comments:

Post a Comment