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).



3 Answers 3


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

  • 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, 2012 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, 2013 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, 2013 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, 2013 at 20:32

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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