PHP sessions in a load balancing cluster - how?

后端 未结 10 1959
粉色の甜心
粉色の甜心 2020-11-29 17:44

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.

相关标签:
10条回答
  • 2020-11-29 18:04

    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.

    0 讨论(0)
  • 2020-11-29 18:06

    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.

    0 讨论(0)
  • 2020-11-29 18:11

    you could also try using memcache as session handler

    0 讨论(0)
  • 2020-11-29 18:12

    When it comes to php session handling in the Load Balancing Cluster, it's best to have Sticky Sessions. For that ask the network of datacenter who is maintaining the load balancer to enable the sticky session. Once that is enabled you'll don't need worry about sessions at php end

    0 讨论(0)
  • 2020-11-29 18:16

    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.

    0 讨论(0)
  • 2020-11-29 18:20

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

    Using redis you are fault tolerant. From my point of view, it could be better than memcache solutions because of this robustness.

    0 讨论(0)
提交回复
热议问题