views:

55

answers:

2

Hello, Here's a little background, currently i have

  • 3 web servers
  • one db server which also host memcache for php sessions for the 3 web servers.

I have the php configs on the 3 servers to point to the memcache server for sessions. It was working fine until alot of connections were being produced for reads etc, which then caused connection timeouts.

So I'm currently looking at clustering the memcache on each web server for sessions, my only concern is how to go about making sure that memcache on all the servers have the same information for sessions.

Someone guided me to http://github.com/trs21219/Memcached-Library because i am using codeigniter but how do i converge my php sessions onto this since memcache seems as a key-value store? Thanks in advance.

Has anyone checked out http://repcached.sourceforge.net/ and does it work?

+1  A: 

There is no need to use some 3rd party libraries to organize memcached "cluster".

http://ru.php.net/manual/en/memcached.addserver.php

Just use this function to add several servers into the pool and after that data will be stored and distributed over those servers. The server for storing/retrieving the data for the specific key will be selected according to consistent key distribution option.

So in this case you don't need to worry about "how to go about making sure that memcache on all the servers have the same information for sessions"

zerkms
But how do i add servers to the pool if im using php.ini to handle my sessions ?
William Smith
Oh, sorry then - i did not get this from the question. I was confused by your addition about "memcached-library" and "repcached" - that's why i thought you're using not native support. Then I think the best solution is to implement your custom session handler using `session_set_save_handler()`, it is trivia task, i'm sure you know that. And in your session handler implementation you can use pool certainly then.
zerkms
When using session_set_save_handler() does php still generate a session id for each user? and would i just code my custom session handler to write the information to all memcache servers?
William Smith
You just implement small set of functions and give `session_set_save_handler()` points to them. After that - you work with sessions as usual: `session_start()` to begin and `$_SESSION['blabla'] = 'foobar';` to interact. So the code that uses sessions don't need to be rewritten.
zerkms
+1  A: 

I'm not sure you have the same expectations of memcache that its designers had.

First, however, memcache distribution works differently than you expect: there is no mechanism to replicate stored information. Each memcache instance is a simple key-value store, as you've noticed. The distribution is done by the client code which has a list of all configured memcache instances and does a hash of the key to direct it to one of the instances. It is possible for the client to store it everywhere and retrieve it locally, or for it to hash it multiple times for redundancy, but these are not straightforward exercises.

But the other issue is that memcache is designed for reasonably short-lived data that memcache is allowed to throw away at any time. This makes it really good for caching frequently accessed data that can be a little stale (say up to a few minutes old) but might be expensive to retrieve (such as almost a minute to generate from a query).

PHP sessions don't really qualify for this, in my experience. A database can easily support many thousands of PHP sessions with barely visible traffic, but you need a lot of memcache storage to support the same number: 50k per session and 5000 sessions means close to 256Mb, and then there is all the other data you want to put in there. Not enough storage and you get lots of unexplained logouts (as memcache discards session data when under memory pressure) and thus lots of annoyed users who have to keep logging in again.

staticsan
"The distribution is done by the client code" --- not actually. read my answer.
zerkms
Yeah it is. Each memcached instance has no knowledge and no way of knowing about the other instances you've got running. The selection of which instance is done by the client library. I know: I looked at several and debugged the one I eventually chose to use. Setting up the pool is done *everytime the page executes* because that's how PHP works.
staticsan
So what do you think my best bet is transferring my sessions over to a mysql database and just have clustered?
William Smith
A custom session handler is not hard. See http://www.php.net/manual/en/function.session-set-save-handler.php for more information; the comments seem to cover all the gotchas. The main two are initializing the custom handler before the first `session_start()` and forcing the session closed before your database handler is closed.
staticsan
Btw, just have seen: 50k per session?!?!?! Looked at some my projects: maximum size for my sessions is 400bytes. Average - 250-300bytes.
zerkms
A few years ago, I looked after an intranet application that needed to put a lot of stuff in the session. 50k was on the high side, but it often came close. If I was writing it now, I doubt I'd put so much in there.
staticsan
Yea the issue here is the db server isn't taking having large amount of ports open well.
William Smith
Two things you can do for that: 1. lower `wait_timeout` to a realistic value (MySQL's default is extremely high). 2. raise `max_connections` to suit the actual traffic.
staticsan
Thanks for the info, and wait_timeout will close all these tcp connections i see in time_wait status?
William Smith
It will close them much sooner, yes.
staticsan