Load Balancing And Session Management In Yii

How can i manage sessions between 2 or more web servers are using to manage Load balancing ?

The points that i found are

Use database session CDbHttpSession

Use cache session CCacheHttpSession

Use Security manager CSecurityManager

You can use memcached for that.

Thank you .

ORey , is it successfully used by anyone?

The application should support web farms i.e. when multiple web servers are used to handle the load .

Well, memcached can use external IP, so why not?

Basically you just configure your apps to use the same memcache server, and that’s it.

(probably you’ll also need to check that the same session id is generated by your servers)

okay. Thank you .

Please see the topic Multiserver . this is the issue I am facing .

And in this topic Yii forum post

see Gustavo 3rd reply .

You have a couple of options:

  1. If your load balancer supports it, you can enable session persistence so that the user always is sent to the same server as the one they originally hit. The benefit of this is that it’s easy to setup if you don’t want to change any code. The downside is that if one of your servers goes down you lose all your sessions on that node.

  2. Setup a shared memcache (not memcached) session between node1 and node2. The relevant settings being.


session.save_handler memcache

session.save_path tcp://<ip1>, tcp://<ip2>


memcache.allow_failover 1

memcache.default_port   11211

memcache.hash_strategy  standard

memcache.max_failover_attempts  20

It’s a little tricky to setup, but once you get it working you have full redundancy between both servers if one were to go down.

  1. Setup a third node to manage sessions and configure php session.save_path to be that server’s ip. The benefit of this is that sessions are now managed by a third server. The downside being you lose redundancy, if that server goes down you lose sessions.

This is the best answer that i got . But I cant Use APC !!

Is there any other methods ?

As Yii project Lead Qiang said , There is only one thing you need to be careful, that is the validationKey of CSecurityManager. By default, this key is automatically/randomly generated the first time and is stored under runtime directory. In multiple server environment, you should explicitly configure this property so that all servers share the same key. This key is used widely to generate hash keys for various security-related measures.

You’ll need to use a non default php session handler. This goes beyond the scope of Yii only. I cannot give more information right now, as I’m writing via mobile phone. There are commercial products (load balancer among the rest), there are open source solutions as well, among which setting session handler via the Db. I never tested this with Yii though.

ok. Thank you. please be detailed if you have time,when you are free.