More and more websites are using PHP sessions. In my opinion session support is one of the coolest features of PHP. HTTP is in its nature a stateless protocol and with PHP sessions you are able to preserve data across subsequent requests. With PHP sessions you can easily maintain state in your web-application. For example a PHP session is a good place to store a user’s shopping cart or user data like name and address when a user logs in.
Let’s say you are running a nice little E-Shop and are using PHP sessions extensively. When your business increases you can’t rely on a single web server any more and maybe some extra CPU power would be very welcome. You are probably thinking about clustering but what about the PHP sessions? Once a session is created it is stored on disk for reuse on subsequent requests. What happens if a user, browsing your E-Shop, switches from one server to another in one of those subsequent requests. Yes you’re right, the session will be empty and so is the user’s shopping cart. The session data, which is stored on disk, is only accessible on the server which created the session. This means that if your E-Shop is running on two servers and you’re planning to use techniques like DNS Round Robin for load balancing you will end up with some very strange behaving shopping cart.
To solve the problem describe above you could spend a lot of money on a load balancer which can keep the user from switching to another server. Once a user enters your E-Shop the load balancer will create a persistent connection to one of your servers and keeps this in memory until some lifetime has expired. This is called ‘session affinity’ and often this is IP or Cookie based. Luckily there are some cheaper solutions. For example you could store the session on a network filesystem which is accessible by all servers. Or maybe you can create a custom session handler and store the session in a database. But when your business increases even more this is going to be a big performance penalty to your E-Shop.
Zend came up with a very nice solution called ‘Session Clustering’. Session clustering is part of the Zend Platform which will probably be discussed many more by me or one of my colleagues. With Zend’s session clustering you are able to use simple load balancing techniques like DNS Round Robin. When using session clustering there is a session clustering daemon installed on every server. These daemons are responsible for storing and distributing sessions. Every session has a unique session id and with session clustering this id also holds information like the server which holds the session data. With this information the session clustering daemon is able to load the session data from another server in your cluster. Session clustering comes with different storage models. You can store sessions in memory or save them on disk for some fault tolerance.
Session clustering is, compared to storing session in a database or network file system, a very fast and scalable solution. When your web-cluster grows there is no increase of overhead from the session clustering daemon. I won’t go into the details too much but if you are interested and want more information don’t hesitate to contact me by e-mail.