1
votes

I am trying to setup memcached to allow session sharing on 2 load balanced Apache servers - in my php.ini I have:

Node 1

session.save_handler = memcached
session.save_path = "tcp://NODE1_IP:11211"

and

Node 2

session.save_handler = memcached
session.save_path = "tcp://NODE2_IP:11211"

In /etc/sysconfig/memcached, I have:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"

In phpinfo I have:

session.save_handler    memcached   memcached
session.save_path   tcp://NODE1_IP:11211    tcp://NODE1_IP:11211

When I login to my application I have session_start on the login page and on the page which checks if the user is logged in but when I:

print_r($_SESSION);

Nothing gets shown as the session looks like it is not being set.

One thing of note is that in phpinfo I see at the top of the session section, these lines:

Session Support enabled
Registered save handlers    files user
Registered serializer handlers  php_serialize php php_binary wddx

Registered save handlers lists files and user but nothing about memcached - might that be why this is not working? How can I change/add to that?

Should the node1_ip be the public or private IP or doesn't it matter?

I can telnet to localhost 11211 but not to NODE1_IP 11211 - get connected refused. Port 11211 is open - tried changing /etc/sysconfig/memcached to "-l NODE1_IP:11211" but still get connection refused

If I change the options in /etc/sysconfig/memcached to nothing i.e. OPTIONS "" then I can telnet to NODE1_IP 11211 so it appears the format of what I am putting into memcached config is wrong?

Is there anything else I need to do? I have tried changing the save paths, adding and removing tcp, using 127.0.0.1, using actual IP addresses, using all the IP addresses seperated with a comma but still cannot get this to work.

2
How do you sync memcached on NODE1_IP and NODE2_IP?Alex Blex
Have tried setting session.save_path = "tcp://NODE1_IP:11211, tcp://NODE2_IP:11211" on both nodes as well as in OPTIONS in /etc/sysconfig/memcached but this doesn't seem to make any differencebhttoan
@bhttoan I had a real nightmare getting memcached to work behind my load balancer and ended up switching to redis in the end. I had an easy time of it because my application was built with Laravel. Out of interest, are you using a framework at all?Spholt
Try to point both servers to the same memcached instance to confirm the handler works. Write something to the session, and check stats items returns the keys.Alex Blex
iirc save_path should be a host:port. Try session.save_path = "NODE1_IP:11211" without tcp.Alex Blex

2 Answers

1
votes

If you have:

session.save_handler = memcached

It means you are using memcacheD extension and not memcache one.

And in that case, the config should be written without the tcp:// protocol:

session.save_path NODE1_IP:11211

session.save_path=tcp://NODE1_IP:11211 is for the memcache extension, without "d" at the end.

0
votes

Assuming you are using sticky sessions on load balancer, and cannot guaranty requests from the same client are served by the same apache.

OPTIONS="-l 127.0.0.1" tells memcached to listen on local loopback only, i.e. it is not accessible from anywhere but localhost. You need to change that to listen on the private network interface (be sure it is not publicly accessible from the internet).

To keep session available on both apache servers, you need to sync memcached. The simplest way is to replicate all data by pointing save_path to both servers, and set session_redundancy to 3:

session.save_path = "tcp://NODE1_IP:11211, tcp://NODE2_IP:11211"
memcache.allow_failover=1
memcache.session_redundancy=3

so the session will be saved to both memcached servers.

If you don't want to sacrifice memory for redundancy, leave only 1 memcached running, and point both apache servers to it.

EDIT :

php_memcache library should be installed, e.g. for debian distros:

php 5.x: apt-get install php5-memcache

php 7.x: apt-get install php-memcache