Can a pool of memcache daemons be used to share sessions more efficiently?

Posted by Tom on Server Fault See other posts from Server Fault or by Tom
Published on 2010-07-27T06:26:01Z Indexed on 2011/01/08 22:55 UTC
Read the original article Hit count: 288

Filed under:
|
|
|
|

We are moving from a 1 webserver setup to a two webserver setup and I need to start sharing PHP sessions between the two load balanced machines. We already have memcached installed (and started) and so I was pleasantly surprized that I could accomplish sharing sessions between the new servers by changing only 3 lines in the php.ini file (the session.save_handler and session.save_path):

I replaced:

session.save_handler = files

with:

session.save_handler = memcache

Then on the master webserver I set the session.save_path to point to localhost:

session.save_path="tcp://localhost:11211"

and on the slave webserver I set the session.save_path to point to the master:

session.save_path="tcp://192.168.0.1:11211"

Job done, I tested it and it works. But...

Obviously using memcache means the sessions are in RAM and will be lost if a machine is rebooted or the memcache daemon crashes - I'm a little concerned by this but I am a bit more worried about the network traffic between the two webservers (especially as we scale up) because whenever someone is load balanced to the slave webserver their sessions will be fetched across the network from the master webserver. I was wondering if I could define two save_paths so the machines look in their own session storage before using the network. For example:

Master:

session.save_path="tcp://localhost:11211, tcp://192.168.0.2:11211"

Slave:

session.save_path="tcp://localhost:11211, tcp://192.168.0.1:11211"

Would this successfully share sessions across the servers AND help performance? i.e save network traffic 50% of the time. Or is this technique only for failovers (e.g. when one memcache daemon is unreachable)?

Note: I'm not really asking specifically about memcache replication - more about whether the PHP memcache client can peak inside each memcache daemon in a pool, return a session if it finds one and only create a new session if it doesn't find one in all the stores. As I'm writing this I'm thinking I'm asking a bit much from PHP, lol...

Assume: no sticky-sessions, round-robin load balancing, LAMP servers.

© Server Fault or respective owner

Related posts about php

Related posts about Performance