Setting Up Memcache with transparent session failover

Setting Up Memcache with transparent session failover

by Jeff White -
Number of replies: 2
I wanted to share this with the community to hopefully help save time if you are experimenting with memcache for session handling.


I will not go into installing memcache and setting up multiple memcached instances on the same server but here are some guides if you need help with that.

I am currently placing memcache on the 2 apache nodes but keep in mind that the save paths are not weighted so the apache node will either go to the first or second node to look for the session. Do not put your application cache and session cache on the same instances!!

What to put in your moodle/config.php

//   Memcache session handler (requires memcached server and memcache extension):

      $CFG->session_handler_class = '\core\session\memcache';

      $CFG->session_memcache_save_path = 'apache01.XXX.XXXX.com:11211,apache02.XXX.XXXX.com:11211';

      $CFG->session_memcache_acquire_lock_timeout = 120;


What to put in your /etc/php.d/memcache.ini file:

memcache.allow_failover=1

memcache.session_redundancy=3

memcache.hash_strategy=consistent

Note: The integer will be the number of memcached instances + 1 for memcache.session_redundancy. If you use the count of memcache instances without +1 then you will get a redirect loop if one of the instances is unreachable (like the node went down). 

restart memcached and apache.

Now test things out!

  1. Login to your Moodle instance on your browser.
  2. In command line on apache01: service memcached stop
  3. Navigate around your page. You are still logged in. 
  4. In command line on apache01: service memcached start
  5. On your browser: refresh your page or navigate around. This is important as some kind of user action must occur for the session to be reloaded to the newly started instance!
  6. In command line on apache02: service memcached stop
  7. Navigate around your page. You are still logged in. 
  8. In command line on apache02: service memcached start
  9. On your browser: refresh your page or navigate around.

So far I have not found any way to have it where a newly started memcache instance will contact the failover instances to reload the session/data. So if you restart one apache/memcached node, wait till it is back up, restart the other node, and a user has not done any action between the restarts he or she will be logged out! 

Average of ratings: Useful (3)
In reply to Jeff White

Re: Setting Up Memcache with transparent session failover

by Jarno Huuskonen -

> So far I have not found any way to have it where a newly started memcache instance will contact the failover instances to reload the

> session/data. So if you restart one apache/memcached node, wait till it is back up, restart the other node, and a user has not done

> any action between the restarts he or she will be logged out! 


You can try starting the node with memcached port blocked with iptables and in memcached init/systemd script (ExecStartPost):

  • use memcached-tool other.node.ip:11211 dump 2>/dev/null | netcat -w10 127.0.0.1 11211
  • remove iptables rule that blocks port 11211 access from other hosts

This is still not perfect, there's still some time (between dump/iptables rule remove) that updates can go only to other memcached node. (If you're interested I could try to find my scripts for doing this).


Other option could be the various repcached patches for memcached, but I'm not sure if these are maintained for recent memcached versions (AFAIK debian has most recent patch ?).

Having a fast and fault tolerant session store with php seems ... challenging sad

Has anybody tested aerospike+php sessions (http://www.aerospike.com/docs/client/php/) to see how it handles failures ?

In reply to Jarno Huuskonen

Re: Setting Up Memcache with transparent session failover

by Jeff White -
I would be very interested in seeing your script Jarno smile 


Since you mention aerospike. I am going to go check that out and play with it on my test boxes.