PHP sessions in a load balancing cluster - how?

OK, so I have a completely rare unique load-balanced PHP website script. Bummer - this was not load balancing. Now we are starting to get problems ...

Currently, the only problem is with PHP sessions. Naturally, no one thought about this problem at first, so the configuration of the PHP session remained by default. Thus, both servers have their own small table of session files, and grief is the user who receives the next request thrown to another server because he does not have the session that he created on the first.

Now I read the PHP manual on how to solve this situation. There I found a nice session_set_save_handler() function. (And, coincidentally, this section is on SO). In addition, I will have to call this function on all pages of the website. And the developers of future pages would have to remember that they’ve been calling all this time. He feels awkward, not to mention the fact that he probably violates the top ten best coding methods. It would be much nicer if I could just flip some global configuration option, and Voilà - sessions that are all magically stored in a database or memory cache, or something like that.

Any ideas on how to do this?




Added: To clarify - I expect this is a standard situation with a standard solution. Fyi. I have a MySQL DB. Surely there should be some kind of ready-to-use code that solves this? I can, of course, write my own material to save the session, and the auto_prepend option specified by Greg seems promising - but it will be like rethinking the wheel.: P


Added 2: Load balancing is based on DNS. I'm not sure how this works, but I think it should be something like this .


Added 3: OK, I see that one of the solutions is to use the auto_prepend option to insert a call into session_set_save_handler() in each script and write my own DB client, possibly to make memcached calls for better performance. Fair enough.

Is there a way that I could not encode all of this at all? Like some famous and proven PHP plugin?

Added a lot, much later: In the end, I went: How to properly implement user session persister in PHP + MySQL?

In addition, I just turned on the session handler manually on all pages.

+43
php session cluster-computing load-balancing
Jun 15 '09 at 7:52
source share
10 answers

You can configure PHP to handle sessions in the database, so all of your servers use the same session information, since all servers use the same database for this.

A good tutorial for this can be found here .

+31
Jun 15 '09 at 9:04
source share

How we handle this is memcached. All that is required is a php.ini change, 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 the domain, but I'm sure it will look like a local memcached.

This method does not require application code changes.

+17
Oct 24 '12 at 15:20
source share

You did not mention what technology you use to balance the load (software, hardware, etc.); but in any case, the solution to your problem is to use sticky sessions on the load balancer.

Thus, this means that when the first request comes from a "new" visitor, a specific server from the cluster is assigned to it: all future requests for the lifetime of their session are then sent to this server. In practice, this means that applications written to run on a single server can be scaled to a balanced environment with zero / small code changes.

If you are using a hardware balancer such as a Radware device, then sticky sessions are configured as part of the cluster setup. Hardware devices usually provide you with smaller-scale control: for example, which server is assigned to a new user (they can check the health status, etc. And choose the healthiest / least used server) and have more control over what happens when the server fails and leaves the cluster. The disadvantage of hardware balancers is the cost, but they are worth what IMHO.

As for software balancers, it comes down to what you use. For Apache, there is a stickysession property on mod_proxy - and many articles via google to make it work with a php session ( for example )




Edit: From the other comments posted after the original question, it appears that your “balancing” is done through Round Robin DNS, so the above probably won't apply. I will refrain from commenting further and start a flame against round dns.

+6
Jun 15 '09 at 8:48
source share

The easiest way is to set up a load balancer to always send the same session to the same server.

If you still want to use session_set_save_handler , you can take a look at auto_prepend.

+3
Jun 15 '09 at 8:10
source share

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

Essentially for each page, we check to see if we know the session identifier. If we don’t check whether we are in a situation that you described, checking to see if sesion data is stored in memory. Otherwise, we just start a new session.

Obviously, this requires that all relevant data be copied to the database, but if you encapsulate your session data in a separate class, then it works fine.

+1
Jun 15 '09 at 8:27
source share

you can also try using memcache as a session handler

+1
Jun 15 '09 at 8:51
source share

If you use php sessions, you can share with NFS in the / tmp directory where, I think, the sessions are stored between all the servers in the cluster. Therefore, you do not need a database.

Edited: you can also use an external service, such as memcachedb (persistent and fast), and save session information in the memcachedb index and specify it using a content hash or even a session identifier.

+1
Jun 15 '09 at 12:02
source share

If you have the time and you still want to check out more solutions, take a look at http://redis4you.com/articles.php?id=01 ..

Using redis is fault tolerant. From my point of view, it may be better than memcache solutions because of this reliability.

+1
Jul 13 '13 at 14:06
source share

It may be too late, but check this out: http://www.pureftpd.org/project/sharedance

Sharedance is a high-performance server for centralizing ephemeral keys / data pairs on remote hosts, without the overhead and complexity of the SQL database.

It is mainly intended for sharing caches and sessions between the server website pool. Access to the data exchange server is trivial with a simple PHP API and it is compatible with the expectations of PHP 4 and PHP 5 session handlers.

0
Dec 03 '13 at 10:29
source share

When it comes to handling a php session in a load balancing cluster, it is best to have Sticky Sessions. To do this, ask a data center network that supports a load balancer to enable a sticky session. When this is resolved you don't need to worry about sessions on php end

0
Jan 13 '17 at 8:20
source share



All Articles