Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP sessions in a load balancing cluster - how?

OK, so I've got this totally rare an unique scenario of a load balanced PHP website. The bummer is - it didn't used to be load balanced. Now we're starting to get issues...

Currently the only issue is with PHP sessions. Naturally nobody thought of this issue at first so the PHP session configuration was left at its defaults. Thus both servers have their own little stash of session files, and woe is the user who gets the next request thrown to the other server, because that doesn't have the session he created on the first one.

Now, I've been reading PHP manual on how to solve this situation. There I found the nice function of session_set_save_handler(). (And, coincidentally, this topic on SO) Neat. Except I'll have to call this function in all the pages of the website. And developers of future pages would have to remember to call it all the time as well. Feels kinda clumsy, not to mention probably violating a dozen best coding practices. It would be much nicer if I could just flip some global configuration option and Voilà - the sessions all get magically stored in a DB or a memory cache or something.

Any ideas on how to do this?


Added: To clarify - I expect this to be a standard situation with a standard solution. FYI - I have a MySQL DB available. Surely there must be some ready-to-use code out there that solves this? I can, of course, write my own session saving stuff and auto_prepend option pointed out by Greg seems promising - but that would feel like reinventing the wheel. :P
Added 2: The load balancing is DNS based. I'm not sure how this works, but I guess it should be something like this.
Added 3: OK, I see that one solution is to use auto_prepend option to insert a call to session_set_save_handler() in every script and write my own DB persister, perhaps throwing in calls to memcached for better performance. Fair enough.

Is there also some way that I could avoid coding all this myself? Like some famous and well-tested PHP plugin?

Added much, much later: This is the way I went in the end: How to properly implement a custom session persister in PHP + MySQL?

Also, I simply included the session handler manually in all pages.

like image 632
Vilx- Avatar asked Jun 15 '09 07:06

Vilx-


People also ask

How do you handle sessions in load balancer?

A load balancer creates sticky sessions by either tracking a user's IP details or using a cookie to assign that user an identifying attribute. This allows the load balancer to use the tracking ID to route all of that user's requests to a specific server throughout the session.

Does session work on load balancer?

Session stickiness, a.k.a., session persistence, is a process in which a load balancer creates an affinity between a client and a specific network server for the duration of a session, (i.e., the time a specific IP spends on a website).

How do PHP manage the sessions?

Modify or Remove a Session You can use unset() to remove a single variable or use session_unset() to remove all variables for a session. You can also use session_destroy() to destroy the session completely. By default, a session lasts until the user closes his browser. This option can be changed in the php.

What is session Clustering PHP?

Session Clustering is a PHP session storage mechanism which uses a network of independent daemons running on frontal web servers to store and share sessions across the cluster.


2 Answers

You could set PHP to handle the sessions in the database, so all your servers share same session information as all servers use the same database for that.

A good tutorial for that can be found here.

like image 159
Oliver Friedrich Avatar answered Sep 25 '22 22:09

Oliver Friedrich


The way we handle this is through memcached. All it takes is changing the php.ini similar to the following:

session.save_handler = memcache session.save_path = "tcp://path.to.memcached.server:11211" 

We use AWS ElastiCache, so the server path is a domain, but I'm sure it'd be similar for local memcached as well.

This method doesn't require any application code changes.

like image 40
Doug Johnson Avatar answered Sep 26 '22 22:09

Doug Johnson