views:

38

answers:

1

If I am running more than two instances of a server (using rackspace-cloud's ip groups), how do I manage my sessions with PHP?

Is there a way to make users 'sticky' to the server they logged into originally? I do use memcached, but all of the cloud systems have memcached on them, I need to insure a users session gets to the right server.

I do not want a single point of failure.

+2  A: 

Use something other than files for session management. PHP allows you to overwrite the handler. I use memcache. There is a PECL extension for it as well: http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/

Here's another article on it: http://www.ducea.com/2009/06/02/php-sessions-in-memcached/

UPDATE

To address issues from the comments:

This allows you to use a central set of memcache machines for sessions. Instead of each server looking locally at its filesystem, it will look to a central memcache cluster you define.

The memcache cluster can be as many machines as you like, to avoid a single point of failure. Here is an example config from php.ini:

extension=memcache.so memcache.allow_failover = 1 memcache.redundancy = 1 memcache.session_redundancy = 2 ; Use memcache as a session handler session.save_handler = memcache ; Use a comma separated list of server urls to use for storage: session.save_path="udp://:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

Mike Sherov
I use memcache all the time, my problem is- there will be many copies of the same system. How do I make a session sticky? All of the cloud devices will have memcache running... know what I mean?
Mike Curry
@mike, you can make each machine reference one central memcache machine.
Mike Sherov
What I really wanted to do is use MemcacheD, on all of the instances, and figure out a way to insure that it can get its session-id using some sort of sticky bit.
Mike Curry
@Mike, this is exactly what I've linked to does. The session id is always stored on the users computer in a cookie (this is how sessions work). The session id is then mapped to a memcache key. If all of your php machines are accessing one central memcache machine (not their own, but a central one), then no matter which php machine they hit, their session persists.
Mike Sherov
@Mike Curry, So long as you configure your memcached server pool correctly, memcached will handle the hashing and retrieval for you. No need to try and stick a session to any one server.
jasonbar
I don't want to have a single point of failure, which is what you've suggested.
Mike Curry
@Mike Curry, no. If you want to avoid a single point of failure, get two central memcache machines. What I've suggested supports that as well. It does redundancy automatically. Did you even look at the article I posted?
Mike Sherov
@Mike Curry, from the second article: "After restarting apache, it will start using memcache to store the php sessions. If redundancy is needed (why not?) we will probably want to use the internal php memcache support to save the sessions to more servers, or if you prefer you can use an external solution to replicate the memcached server data – repcached."
Mike Sherov
@Mike Curry, updated my answer to show more clearly how to address all the concerns you raised.
Mike Sherov