PHP Memcache Sessions & Redundancy

I started using memcache to store sessions, rather than having PHP store them on disk. The server hard drives are SSD so I never noticed any performance issue sorting them on disk, but I did not like all those files filling up my /tmp space.

Once moved to memcache, then you have the issue of redundancy. If you have more than one server handling your traffic load, you need something to maintain a sticky session or the user would be logged out of your site (or session information lost) if they move between servers.

Doing some reading there seems to be a lot of bad information out there about exactly how to setup session redundancy across multiple memcache servers.

On a lot of sites I found this syntax to use:

tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15

I think that syntax is not correct, but it is that way on many sites. According to the documentation you would not encode the &. In addition all the values they are listing are the defaults. So odds are good those params are not valid like that, but it still works because the values are the default.

One of the better articles I found online was this one at DigitalOcean.

How To Share PHP Sessions on Multiple Memcached Servers on Ubuntu 14.04

His configuration is a bit different than mine, since the OS is different. A couple things to emphasize if you are trying to set this up.

In his example using three servers he says to place the following on each server:

session.save_path = 'tcp://10.1.1.1:11211,tcp://10.2.2.2:11211,tcp://10.3.3.3:11211'

The order here is important, I think a lot of people will want to change the order of the servers, placing the local server first. Don’t do that! In order for the redundancy to work correctly the session.save_path must be the same on all servers. Do not worry about the order, as PHP must contact each server to write the session data anyway.