I currently have a huge problem. Two days ago my site running on one server was too much, so I purchased two more and had them clustered (rsync and load balanced).
I then start noticing that a user would hit server-1 and then on the next request hit server3 but that their session was still on server1 instead of server3 and they were no longer logged in.
I was recommended to use memcache for session stores.
My script already uses $_SESSION
.
Can we get memcache installed and enable session handler support and set session.save_handler = "memcache"
to force php to use memcache?
Is there any application programming that needs to be done to use memcache?
Will this solve my session between server issue?
Are the session stores stored on all the servers when they are created or is one like a master memcache server?
I'm using the codeiginiter framework
There are two parts to the question:
This is down to your load balancer/reverse-proxy. It's common to make clients stick to one server, usually by IP address or a transparent cookie set by the proxy. However, it's not necessary to have client stickiness for the sake of sessions if you have a distributed session store, which brings us to memcache.
memcache has a proper shared-nothing distributed architecture, so most of the intelligence is at the client end. So what you should do is go ahead and use the memcache session storage, but instead of pointing at one server, point it at ALL of them. This is covered in the docs. In your php.ini you should set session.save_path to the list of memcached servers, for example server1:11211, server2:11211
.
Be aware that there are two distinct memcache client libraries available in PHP called memcache
and memcached
and they have different syntax for this property.
Because of the way that memcache works, you don't care where your session data is stored - it's taken care of for you.
As NathanD points out, memcache is volatile and loses data on a restart, and when you have multiple servers this would mean that some (but not all) of your users would be logged out if one was restarted. If one server dies completely your session storage will stay working. Users whose session data was on the dead server will be kicked off, but they can log back in and carry on without that server being present.
Both of the major memcache PHP PECL extensions have session handlers. Either will require you to install a PECL module before use.
The Memcache PECL extension session handler is enabled with the following in php.ini:
session.save_handler = "memcache"
session.save_path = "tcp://memcacheServerAddressHere:11211?persistent=1&weight=2&timeout=2&retry_interval=10"
The Memcached PECL extension session handler is enabled with the following in php.ini:
session.save_handler = "memcached"
session.save_path = "memcacheServerAddressHere:11211"
Note that the Memcache extension appears to allow more configuration of the Memcache environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With