views:

45

answers:

3

All,

I have a PHP5 web application written with Zend Framework and MVC. This application is installed on 2 servers with the same setup. Server X has php5/MySql/Apache and Server Y also have the same. We don't have a common DB server between both the servers.

My application works when accessed individually via https on Server X and Server Y. But when we turn on load balancing and have both servers up, the sessions get lost.

How can I make sure my sessions persist across servers? Should I maintain my db on a third server and write sessions to it? IF so, what's the easiest and most secure way to do it?

Thanks

+1  A: 

memcached is a popular way to solve this problem. You just need to get it up and running (easy) and update your php.ini file to tell it to use memcached as the session storage.

In php.ini you would modify:

session.save_handler = memcache
session.save_path = ""

For the general idea: PHP Sessions in Memcached.

There are any number of tutorials on setting up the Zend session handler to work with memcached. Take your pick.

jasonbar
A: 

Should I maintain my db on a third server and write sessions to it?

Yes, one way to handle it is to have a 3rd machine running the database that both webservers use for the application. I've done that for several projects in the past and its worked well. The question with that approach is... is the bottleneck at the webservers or the database. If its at the database, you wont see much improvement by throwing load balancing of the web servers into the mix. You may need to instead think of mirroring schemes for the database.

GrandmasterB
Can you provide an example for storing sessions in DB from your past work?
Vincent
I use a home grown class that handles sessions manually (by sending the cookie headers manually, rather than letting PHP do it), so I dont know how much help that would be to you. If you want to use PHP's session handling, review the session handling functions in the php docs. You want to assign a session_set_save_handler() that writes the session data to a database table. And then a corresponding read function. I wish I had access to my older code that used php's session handling so I could send you them, but alas, I dont anymore.
GrandmasterB
Here's something along the lines of what I used to do... this may be dated, I dont know, since I havent used PHP's session handling in a while. http://www.tuxradar.com/practicalphp/10/3/7 Just make sure you put an index on the session id or it could be slow!
GrandmasterB
A: 

Another option is to use the sticky sessions feature on your load balancer. What this will do is keep users on certain servers. So when user 1 comes to the site, they will be directed to server X. Every subsequent request will also be directed to server X. This allows you to not worry about persisting sessions between servers, as each user will continue to be directed to the server they have their session on.

The one downside of this is that when you take a web server out of the pool, half the users with a session will be logged out. So the effectiveness of this solution depends on how often you take servers out of the pool.

Chris Henry