php-memcached-dev / php-memcached

memcached extension based on libmemcached library
Other
987 stars 323 forks source link

Sessions replications for high availability (not working as expected) #349

Open jeffjrare opened 7 years ago

jeffjrare commented 7 years ago

Hi!

I have a php5.6 application and I want to save the user sessions to memcached using pecl-memcached. Saving the session to 1 memcached server is not a problem.

What I want to do is to have 2 memcached (named memcached01 and memcached02)server for high availability. Reading the doc leads me to think that it is possible.

Here are the config I played with (everything else use default values) :

session.save_handler = memcached
session.save_path = "server1:11211,server2:11211"
memcached.sess_consistent_hash = On
memcached.sess_remove_failed = On
memcached.sess_number_of_replicas = 1
memcached.sess_binary = On
memcached.sess_connect_timeout = 50

I am using centos7 with IUS to get php56 and pecl-memcached :

php56u-pecl-memcached-2.2.0-6.ius
.centos7.x86_64
libmemcached-1.0.16-5.el7.x86_64

I keep it simple for testing purpose and only use 1 web server (named web01).

Here are the steps I did:

...From what I read (and it is very possible that I am wrong), using memcached.sess_consistent_hash = On make it so that the web server always connect to the same memcached server even though it also replicate the session to the other memcached server.

In this case, when memcached02 memcached server stoped. It was remove as per memcached.sess_remove_failed = On.

Web01 server started going to memcached01 to read it's session. As soon as memcached02 memcached server came back online. Because of memcached.sess_consistent_hash = On, web01 server started to go back to memcached02 server directly. But since the service restarted the cache is empty and so my application log me out.

I may be wrong but to me it feels like there is something not OK there. When memcached02 server goes down and is removed from the available memcached server should the hash be updated so that web01 now connect to memcached01 server and when memcached02 server goes back online start replicating session to it?

Sorry for the length guys, and thanks for the attention!

nokiomanz commented 7 years ago

I have been using pecl-memcache for years for my application sessions to be highly available. Super simple to put in place and just work great. Any memcached server can drop out/in with no interruption of service.

When I did some test with pecl-memcached to use exactly as I do with pecl-memcache, I never had much success to get the session to be highly available. I always ended up loosing my session like your describing.

I will be very interested to see what others have to say on this.

adminduproprio commented 7 years ago

We have the same problem here with pecl-memcached..

If someone have a solution on this, its will be appreciate.

tdbui22 commented 6 years ago

@jeffgirard your problem is exactly what I am experiencing. With your exact same explanation when I turn sess_consistent_hash off a different type of error occurs.

With it off when I reboot memcached02 the session data is resynced (as it seem to be getting it from memcached01). But when I stop memcached01 now I get the error "PHP message: PHP Warning: Unknown: Failed to write session data (memcached)." as it seem to be only trying to write to memcached01 and completely ignores memcached02.