Apache howto idea memcached OpenSource pecl performance PHP

Using new PECL Memcached extension for storing session data

Many of you already know that managing session is a critical task for web applications, specially when you want to avoid I/O hop and also a significant load over your database by writing a custom session handler. Beside that, if your application makes use of multiple web servers behind a proxy, then its more than a critical job to share and manage session data among these servers effectively. This is why a central session manager is very important for your application to scale. In this article I am going to show you how you can use the latest Memcached extension (developed by Andrei Zmievski and his team) to isolate the session storage from web servers. I will show you how to compile the extension and use it.

Step1: Install Memcached Server
If you are using Debian its just plain simple
[sourcecode lang=”bash”]
apt-get install memcached

Step 2: Run memcached instances
Lets run two instances of memcached in same machine (well, this article is just for demonstrating you how you can get things done. In the production environment, you can deploy as many memcached instances as you want in different servers in same network)
[sourcecode lang=”bash”]
memcached -d -l -p 11211 -u <username> -m 16
memcached -d -l -p 11212 -u <username> -m 16

Above commands will run two instances of memcached listening on port number 11211 and 11212, same IP Each of them get an allocation of 16 MB of memory (on RAM).

Step 3: Install the PECL Memcached extension.
Lets install the new PECL memcached extension in your web server. This new extension depends on libmemcached. You can grab the latest distribution of libmemcached from and compile it in your own machine. Make sure you have the dependencies met.

[sourcecode lang=”bash”]
tar -zxvf libmemcached-0.34.tar.gz
cd libmemcached-0.34
make && make install

Considering everything went fine, lets install the PECL memcached extension
[sourcecode lang=”bash”]
pecl install memcached

If everything goes fine, you should see the output similar like this
Build process completed successfully
Installing ‘/usr/lib/php5/20060613/’
install ok: channel://
configuration option "php_ini" is not set to php.ini location
You should add "" to php.ini

Make sure that is placed in your PHP extension_dir folder (here /usr/lib/php5/20060613). Add the line “” in your php.ini and restart your web server.

To make sure, everything’s done and working – run a phpinfo() and check the output. There should be a “memcached” sesction which will look like the following one.

Memcached PECL Extension
Memcached PECL Extension

Now we need to make change in our php.ini to register Memcached as a session handler and set the necessary properties there. Open your php.ini and add the following two lines. If you find any similar line un-commented, comment them out first.

[sourcecode lang=”php”]

Restart your web server. And …… you are done! 🙂 – Now all your session data will be saved and served from these memcached servers. No matter whenever you need to extend your setup by adding extra web servers, all user data and session data will remain valid and served from a central location. No I/O issue, no huge write load on DB servers.