The high availability of your PHP application can be achieved by implementing PHP sessions clustering in the cloud. To successfully handle application server failover we create environment with Memcached, several Apache servers, and an NGINX load balancer:
The Memcached node is used as a distributed caching engine in the environment with multiple nodes. In our case, it provides a Web session with the sticky sessions running on several application servers. If one server fails, the sessions are stored for backup on the Memcached node. Other servers can fetch the sessions from Memcached and start serving the session.
When each session request is completed, the session is sent to the Memcached node for the backup. This session remains available on the original application server and can be used for serving the next request. After the second request completion, the session is updated in the Memcached.
If the original server fails, the next request is routed to another application server. The newly requested server is asked for the session it doesn’t know. So this new server will find the needed session in the Memcached node. It is identified according to the ID that was appended to the sessionID while session creation. As a result the server can fetch the session from the Memcached node.
When the server answers the request, it also updates the session in the Memcached node. Thus there is no interruption of the app caused by the original server failure – the failover is successfully handled.
At the same time, the NGINX load balancer is distributing traffic across the cluster containing HTTP resources. You can check the load balancing in the CirrusGrid cloud using different load balancing tools.
Navigate to your environment in the dashboard and click Config button for Apache.
In the opened tab go to etc > php.ini
Add the next line to Dynamic Extensions:
extension=memcached.so
4. Make the changes in the [Session] block:
session.save_handler = memcached session.save_path = "< server >:11211"
Note: < server> states for memcached IP or URL which you can find by clicking Info button for the memcached node in your environment.
5. Save the changes and restart the Apache node. That’s all! Now if one of the instances fails, the users get automatically switched to the other instance in this cluster and never notice any change.
Powered by BetterDocs
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.