views:

90

answers:

4

I currently have a huge problem. Two days ago my site running on one server was too much, so I purchased two more and had them clustered (rsync and load balanced).

I then start noticing that a user would hit server-1 and then on the next request hit server3 but that their session was still on server1 instead of server3 and they were no longer logged in.

I was recommended to use memcache for session stores.

My script already uses $_SESSION.

  • Can we get memcache installed and enable session handler support and set session.save_handler = "memcache" to force php to use memcache?

  • Is there any application programming that needs to be done to use memcache?

  • Will this solve my session between server issue?

  • Are the session stores stored on all the servers when they are created or is one like a master memcache server?

I'm using the codeiginiter framework

+5  A: 

Please read dormando's post.

Dustin
tl;dr version: Use a database session handler instead.
R. Bemrose
Good post, but I disagree with the conclusions... You shouldn't use long running session data to keep users logged in for long periods of time anyway (there are better, less resource intensive ways of doing that)... So who cares if the session is reset ever, the user would only ever notice it if they get caught by a CRSF break (where they open the form before the flush, and submit after). 99% of other cases should be transparent. And something to note is that even with the hybrid solution, it still won't prevent the loss of CSRF data (db is only synced every so often). Fixing a non-problem...
ircmaxell
Oh, and you shouldn't keep anything important in sessions anyway (after all, they are going to be deleted at some point)... So why should durable writes matter? And if you need durable writes, go with something like MySQL's NDB which is almost as fast as memcached, but durable... But I think it's trying to solve the wrong problem...
ircmaxell
So where you think is best for users logged in data to be ?
William Smith
+1  A: 

You would need to set up memcache to run on one of the servers and have all of the servers use that same memcache instance for the sessions. Otherwise, if they each run their own memcache instance, you'll have the same problem as before.

Other than configuring memcache accordingly and telling PHP to use it as your session handler, you shouldn't have to make any changes to your code.

konforce
Do you think dedicating 4gb of memory from our server is good enough? we have 24gb each server
William Smith
That seems like a huge number, but it depends on how many simultaneous sessions you have and how much data you store in them. Just doing a directory listing of the /tmp (or wherever the files are currently stored) should give you a good idea of how big your session data is. I just checked a site I run and it uses ~1MB per 200 users, with the median session size only being 85 bytes. But obviously those numbers are only applicable to that particular site.
konforce
+1  A: 

Both of the major memcache PHP PECL extensions have session handlers. Either will require you to install a PECL module before use.

The Memcache PECL extension session handler is enabled with the following in php.ini:

session.save_handler = "memcache"
session.save_path = "tcp://memcacheServerAddressHere:11211?persistent=1&weight=2&timeout=2&retry_interval=10"

The Memcached PECL extension session handler is enabled with the following in php.ini:

session.save_handler = "memcached"
session.save_path = "memcacheServerAddressHere:11211"

Note that the Memcache extension appears to allow more configuration of the Memcache environment.

R. Bemrose
+1  A: 

You want to be careful about doing this. One thing to be careful about is to not host session info with other non-session data. It isn't the biggest deal to clear your cache when it only contains your own site's data but you do not want to wipe out people's sessions along with it.

As long as your are using the same key with memcache you should hit the same server every time. So that issue should go away.

Wix
Exactly the server only has 1 site on it so do you think that will be an issue? we are doing mem cache with a db server
William Smith
You should be fine. You just need to be careful when you want to refresh the server. We have put one of our application's sessions on Memcache and we are very careful about it.
Wix
Thanks for the tip
William Smith