High Availability Joomla, WordPress – Load balance persistant PHP database sessions
Posted by Buzz in mySQL, phpIf you’ve seen the new twitter feed to the right you may of seem some ramblings about ‘cura’.
Cura is a PHP class I have authored in Co-operation with Psycle Interactive (The company I now work for, so be sure to thank them for allowing me to publish this write up!)
So what does it do?
Cura sets several call back objects in your PHP application that re-directs all session data to a mySQL database.
But why do I need that?
The average 1 server end user can stop reading here, as I can tell you now that Cura is not for you.
If however you are a business fielding multiple web servers then read on.
By passing all your PHP sessions to a database you remove the work around requirements for a load balanced solution.
i.e. web1 web2
1) Shopper arrives at web1 and logs in.
2) Shopper adds item to cart, which is logged against their session.
3) web1 is subjected to a search engine index.
4) web2 is now serving the shopper, shoppers basket is now logged out as their session id has changed …
There are numerous work around methods for this, such as having a single shared mount point for the PHP session files, the use of cookies etc …
The problem is in a high availability solution that a single mount point is just that, it’s singular and therefor a single point of failure.
Then there is the use of cookies, which is fine until you start to store a lot of data during your users session, at which point on each server change you are reliant on the cookie data being transmitted back to the server each time, raising the question what is the point of adding a load balanced solution if the user experience becomes degraded due to it’s deployment?
So secret option number 3 is to use a database, you can remove the single point of failure by having a mySQL cluster, and you haven’t got to worry too much about how much data you are storing.
Because everything is in a database whenever your web application is run (web1, web2) it will read the data from one source, allowing persistent sessions across your whole platform without the need for single mount points or session replication.
The source files are available from: http://svn.saiweb.co.uk/branches/cura-php/trunk/
1 | svn co http://svn.saiweb.co.uk/branches/cura-php/trunk/ |
To deploy this solution simply add the following lines to any file that calls session_start();
1 2 3 4 5 | require_once('/path/to/cura.class.php'); $cura = new cura($db, $user, $password, $host); session_start(); ... the rest of your file... |
Ensure that you have created a ‘sessions’ table as per the provided sessions.sql file in your database.
I will be adding simplified support for wordpress and joomla shortly these will become available from: http://svn.saiweb.co.uk/branches/cura-php/trunk/
Entries (RSS)
Joomla1.0 used mysql exclusively for session handling.
Joomla1.5 uses a custom session handler that can stores to a database, or a memory cache etc.
http://api.joomla.org/Joomla-Framework/Session/JSessionStorage.html
Here’s the mysql session storage class.
http://api.joomla.org/__filesource/fsource_Joomla-Framework_Session_joomlasessionstoragedatabase.php.html
Hi Gabe,
“Joomla1.0 used mysql exclusively for session handling. ”
Whilst I can say 1.x does have a table where it stores some information on sessions.
I can infact say without a doubt that 1.0.12 did not, the version I have had to work with was 1.0.12 and all sessions relied on PHP inbuilt file based sessions, this failed miserably when moving to a high availability solution, some information was passed to a mySQL table however there was no reference to the session_set_save_handler(), meaning all sessions were file and not mySQL based.
“Joomla1.5 uses a custom session handler that can stores to a database, or a memory cache etc. ”
I am yet to look at later version as I haven’t had the time, it’s “on the list”.
Thanks for the links I will check them out.
[...] High Availability Joomla, WordPress – Load balance persistant PHP … [...]
I really very liked this post. Can I copy it to my blog?
Thanks in advance.
Sincerely, Your Reader.
Hi Timur,
I’d rather you linked back, rather than just copy pasted what I have written.
Cheers
BUzz