Categorized | CentOS, Fedora, Linux, PHP

Session Stickiness…A thing of the past

Posted on 07 October 2008 by Abidoon

So ever notice some old sites with domains showing up in your address bar that look something like www1.example.com or www2.example.com? This is usually because the site owners are trying to load balance, in a crude way by making you stick to one particular webserver.

Usually the main reason for doing this is because of sessions. For security reasons your browser will not keep you signed in with the same session if you hop subdomains. What this means is that if you logged in on www1.example.com and now go to www2.example.com the browser will consider this as two separate sites and you would have to login again on www2.

People used to play a number of tricks to remove session stickiness such as storing sessions in the db so that they could transparently load balance your session across multiple webservers. However databases due to their inherent nature are often large monolithic beasts that are much slower than some of the alternatives available out there now.

One of these, which I am a fan of, is memcache! The folks over at danga.com have given us one of the most powerful building blocks for creating highly scalable websites.

We can now experience load balancing as it was meant to be!

As an example lets take PHP session handling. PHP usually stores its session on the local hard drive of a server. You can also make it store sessions inside a database, lets say MySQL. However, as I mentioned earlier MySQL is very slow compared to memcache and is inherently harder to scale compared to memcache.

To setup PHP to use memcache for sessions, this is all you have to do :

0 – Install memcache library available at http://pecl.php.net/package/memcache

1 – Open php.ini

2 – Set session.save_handler = memcache

3 – Set session.save_path = “tcp://IP:PORT&weight=X” where X is the weight of a particular memcache server. You can add as many as you like by specifying one after the other separated by a comma.

4 – Install memcached. For a distribution like Fedora or CentOS just execute “yum install memcached”

All of this should not take you more than 5 minutes, and you are done!

Now to scale just keep adding more servers and firing up the memcached daemon  and experience the true beauty of completely transparent load balancing :) .

Tags | , , , , ,

Leave a Reply

 

October 2008
M T W T F S S
« Sep   Feb »
 12345
6789101112
13141516171819
20212223242526
2728293031