views:

2825

answers:

7

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?

+2  A: 

The easiest thing to do is configure your load balancer to always send the same session to the same server.

If you still want to use session_set_save_handler then maybe take a look at auto_prepend.

Greg
The load balancing is somehow done with DNS entries. The hostname resolves to two different IP addresses and the browser picks one randomly. Or something like that - I don't really know the particulars. But AFAIK there is no load balancer. The "auto_prepend" looks promising.
Vilx-
+1 use auto_prepend to insert the session_set_save_handler function before every script
Galen
Sounds like you've got a "Round Robin DNS" on the go - http://en.wikipedia.org/wiki/Round_robin_DNS . I'll say that the least said about this "solution" to load balancing, the better.
iAn
Perhaps, but I didn't choose this nor can I change it.
Vilx-
+1  A: 

When we had this situation we implemented some code that lives in a common header.

Essentially for each page we check if we know the session Id. If we dont we check if we're in the situation whehich you describe, by checking if we have stored sesion data in the DB.Otherwise we just start a new session.

Obviously this requires all relevant data to be copied to the DB, but if you encapsulate your session data in a seperate class then it works OK.

Visage
+1  A: 

You don't mentioned what technology you are using for load balancing (software, hardware etc.); but in any case, the solution to your problem is to employ "sticky sessions" on the load balancer.

In summary, this means that when the first request from a "new" visitor comes in, they are assigned a specific server from the cluster: all future requests for the lifetime of their session are then directed to that server. In practice this means that applications written to work on a single server can be up-scaled to a balanced environment with zero/few code changes.

If you are using a hardware balancer, such as a Radware device, then the sticky sessions is configured as part of the cluster setup. Hardware devices usually give you more fine-grained control: such as which server a new user is assigned to (they can check for health status etc. and pick the most healthy / least utilised server), and more control of what happens when a server fails and drops out of the cluster. The drawback of hardware balancers is the cost - but they are worth it imho.

As for software balancers, it comes down to what you are using. For Apache there is the stickysession property on mod_proxy - and plenty of articles via google to get this working with the php session ( for example )


Edit: From other comments posted after the original question, it sounds like your "balancing" is done via Round Robin DNS, so the above probably won't apply. I'll refrain from commenting further and starting a flame against round robin dns.

iAn
I didn't choose this solution nor can I change it.
Vilx-
+1  A: 

you could also try using memcache as session handler

+4  A: 

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.

BeowulfOF
A: 

If you are using php sessions you could share with NFS the /tmp directory, where I think the sessions are stored, between all the servers in the cluster. That way you don't need database.

Edited: You can also use an external service like memcachedb (persistent and fast) and store the session info in the memcachedb index and indentify it with a hash of the content or even the session ID.

Khriz
This is the current "quick workaround", but the admin doesn't like that. Something about security and stuff, I don't know the particulars.
Vilx-
Mmm, then you could use an external service like memcachedb using a hash of the key as a index for the sessions. The memcachedb service is very fast (especially writing) and easy to use, you can use a special and reduced set of the memcached functions from PHP.
Khriz
did you tried id? sounds risky and unstable.
knoopx
A: 

check out this link, I did and it works great http://www.excedesoft.com/blog/php-sesiones-y-balanceo-de-carga/

terra
Now, if only I understood Spanish (or whatever the language is). :D Anyways, check this: http://stackoverflow.com/questions/1022416/how-to-properly-implement-a-custom-session-persister-in-php-mysql
Vilx-