Monday, June 28, 2010

Setting up memcached distributed caching system

SkyHi @ Monday, June 28, 2010

Problem Statement:


A distributed caching system such as memcached is really a life-saver when it comes to reducing a great deal of READ (SELECT) traffic from MySQL tables that are in-frequently changed. However, memcached can be a object cache for anything. Using a simple key/value (get/set) API you can store anything you want limited by size constraints. Here we will show you how to install memcached from the source distribution and use it in your PHP-based (yup we are a PHP-shop) Web applications.









Time Estimate (30 ~ 60 min)

We estimate that the entire process to get memcached compiled, installed, and tested might take half an hour to an hour.

Step 1: Installing memcached pre-requisites


You will need to install libevent and its development pacakges via yum as follows:


 yum -y install libevent libevent-devel

Step 2: Compiling and installing memcached


Follow the steps below;


  1. Download the memcached source code from: http://www.danga.com/memcached/
  2. Extract the source in /usr/local/src and you should have a new sub directory [memcached-version]
  3. For a default setup, run ./configure from the newly created memcached source sub directory
  4. If configure runs without error, you have all the required pre-requisites and good to go to the next step. Otherwise, you need to

    review the errors and install any pre-requisite library and related header files via yum.
  5. Now run: make and make test and make install in this order as your success continues

Now you have memcached compiled and installed.


Step 3: Setting up memcached to start when you restart your server


Make the following decisions:


  1. First, decide how much memory you want to give memcached to use for caching.
  2. Then decide if you want to run memcached on the default port (11211) or not.
  3. Next decide if you want memcached to listen to a specific IP address if you have multiple IP addresses on your server
  4. Finally decide, what user you want to run memcached as; typically, you want to run it using Apache user so that Apache processes can access memcache data

Say, you decide to use 1GB of RAM for caching and want to run memcached as user ‘httpd’ on default port. In such a case you would run the following command as root:


/usr/local/bin/memcached -d -m 1024 -u httpd

This will start memcached as a daemon process and allocate 1GB of RAM (1024MB) using default port.


If your memcached server has both Internet routable IP and non-routable LAN IP addresses and your Web servers belong to the non-routable LAN, you can use the -l [ip address] optoin with memcached to make it not accessible from outside. For example:


/usr/local/bin/memcached -d -m 1024 -u httpd -l 192.168.1.100

This tells memcacehd to listen only to 192.168.1.100 interface on the server and thus requests from the Internet will not be possible, which makes it more secure. Of course, if your Web server and memcached is running on the same server, you can use the loopback interface (127.0.0.1) to limit memcached expouser to applications running on the local Web server.


Once you know what options you need to use with memcached, you should add the command-line you use to start memcached in /etc/rc.local. This will make sure memcached starts automatically when you restart your server.


#!/bin/sh<br />echo "# Start memcached" >> /etc/rc.local<br />echo "/usr/local/bin/memcached -d -m 1024 -u httpd -l 192.168.1.100" >> /etc/rc.local

The above shell script will put a memcached command in the /etc/rc.local file so that it starts automatically with 1GB of RAM as ‘httpd’ user on a LAN interface.


Step 4: Setting up PHP support for memcached


Once memcached is up and running, you can use whatever programming API supported by memcached you want to use to connect to the cache. Since we are PHP shop, we will use PHP API. To get PHP configured for memcached, simply run the following command as root:


pecl install memcache

This will compile and install a memcached dynamically loadable module for PHP. Your next step is to edit the php.ini (use locate php.ini to locate the one that you are using; default: /usr/local/lib/php.ini) file

and add the following lines at the very end:


; start memcached<br />extension=memcache.so

Save the changes and restart Apache server. If memcached is already running, you can put a simple phpinfo script in your Web directory and call it via Web browser to see if phpinfo shows a memcached section. Such as script is shown below:


#!/bin/sh<br />echo "# Start memcached" >> /etc/rc.local<br />echo "/usr/local/bin/memcached -d -m 1024 -u httpd -l 192.168.1.100" >> /etc/rc.local

A sample output of the above script is shown below in Figure 1:



Figure 1: memcached info reported by phpinfo


Step 5: Testing memcached with a simple PHP scripts


To really know that your memcached setup is working as expected, you can use the following simple PHP scripts.



If you use these scripts to test your memcached setup, make sure you change the

values for MEMCACHED_SERVER amd MEMCACHED_PORT if you are running

the memcached on a different server than localhost and/or on a different port than the default one.


The init_memcached.php script sets a key/value pair in memcache and the read_memcached.php reads

the value using the same key. By setting the key and value using a script and reading it using another,

you know you are able to use a shared cache as expected.


Here is a sample run of the scripts:


$ php init_memcache.php<br />FavoriteBook is set to Childhood's End in memcached.<br /><br />$ php read_memcache.php<br />Got FavoriteBook value: Childhood's End

REFERENCES
http://centoshacker.com/kabir/tuning/setting-up-memcached-distributed-caching-system.html