Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two httpd servers running PHP on RHEL 5.7. Initially I was sharing PHP sessions between the servers using a common NFS share that was hosted on a third server. This was causing slow downs and eventually hangups on the httpd servers.

What is the best practice for sharing PHP session data between multiple httpd front end servers?

The vendor is recommending that I leverage load balancing connection persistence via a cookie or source IP, but I am concerned that a failure in one of the httpd servers will cause users to lose their session data and be logged out (for example).

Thoughts?

Thanks!

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

Either put your sessions into your (hopefully replicated) database, or use replicated memcached servers for your sessions.

share|improve this answer
    
yeah, memcached seems to be the common solution. As for database, if it's not a clustered database, it's once again single point of failure –  Sandman4 Apr 17 '12 at 16:16
    
Memcache or any distributed cache system should NOT be used for session, as key-values may be garbage collected at any time. Prefer a replicated SQL more reliable persistence. –  Tom Desp Mar 31 '13 at 16:26
    
@TomDesp, it depends on requirements, but in general, you want to cap the amount of stored session state to some reasonable level. In that case, the LRU-style behavior of memcache is reasonable. "Live" sessions will not get evicted from the cache in unless you get a massive traffic flood. If you get a massive rtaffic flood, the LAST thing you want is sessions stored in a database. A potentially better method is to use stateless sessions: a cookie is set with a userid+expriation+HMAC. The HMAC uses a server-side secret. That way you can validate and/or recreate an in-memory session as needed. –  rmalayter Jun 10 '13 at 16:45
    
@malayter MemCache may be used for many other — potentially consuming — "real" cache needs. You may run out of MC room for other reasons which I really don't want my sessions to be evicted for. Sessions are important user data that have to be collected with appropriate non-random strategy IMHO. –  Tom Desp Jun 17 '13 at 20:32
add comment

This is a spin off from what I use for various customers:

It's currently ZF-based, but it can be easily extended to work with other frameworks. The idea is to use both Memcache and MySQL as a backup. Since it uses ext/memcache (a lot of people suggest to stick with 2.x-stable) I can add multiple servers have the extension replicates data across them.

Not a 100% sure if this is the best idea, you could adchieve the same in userland code as well.

share|improve this answer
add comment

One lightweight option to test is Scache. But read it's intro, there's locking issues when using $_SESSION in multi server environment.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.