PHP Session Clustering in the Cloud

by Dominik Poetek  • 

Editor’s note: A version of this post originally appeared on the Jelastic blog.

It’s often necessary to extend a PHP application across multiple servers. This is particularly crucial when your business depends on the availability of the application. Extending the app across multiple servers provides redundancy, ensuring that when one server fails, it doesn’t bring down the entire app.

If your application is running on different servers, you need to be able to handle session failover. But it’s also important to have a solution that can scale horizontally. You need to be able to add more servers as necessary to avoid overloading the machines.

Ensuring high availability for your app requires the related strengths of redundancy and scalability. And there is only one way to make sure this happens: a solution that adds clustering to cloud.

Today we will show you how to implement clustering in the ServInt cloud, powered by Jelastic. In our example, we will use memcached, two Apache servers, and an NGINX load balancer. The main use of memcached is as a distributed caching engine in a multinode environment. But for our purposes, imagine a Web session with sticky sessions running on several app servers. If one server fails, the sessions are stored for backup on a memcached node. Other servers can fetch the sessions from memcached and serve the session from then on.


Here is how it works: In normal operation, after each session request is completed, the session is sent to a memcached node for backup. When the next request has to be served, the session is still available on the original application server and can be used. After the second request is finished, the session is updated in the memcached node. But if the original server dies, the next request is routed to another application server. The new server is asked for a session it doesn’t know.

When that happens, the new server will look up the session in the memcached node. It knows where to look based on an ID that was appended to sessionID when the session was created. It can then fetch the session from the memcached node. After the server answers the request, it also updates the session in the memcached node. Thus the original server’s failure causes no interruption of the app, and the failover is successfully handled. During this time, an NGINX load balancer is distributing traffic across the cluster containing HTTP resources. Here is how to use memcached to successfully handle application server failover:

Create an environment

  1. Log in to the Jelastic Manager.
  2. Click the Create environment button.
  3. In the Environment topology window choose two or more servers you want to use (for example, two instances of Apache) and Memcached node. Type the name of the environment and click Create.


In a minute your environment will be created.

Configure application servers

  1. Click the Config button for Apache.
  2. In the opened tab navigate to etc > php.ini
  3. Activation of the php memcached module is very simple, just add the following line to the Dynamic Extensions:


  1. Memcached provides a custom session handler that can be used to store user sessions. A completely separate memcached instance is used for that internally, so you can use a different server pool if necessary. The session keys are stored under the prefix memc.sess.key. Be aware of this if you use the same server pool for sessions and generic caching. Make these changes in the Session block of php.ini to enable sessions support:
session.save_handler = memcached
session.save_path = "tcp://<server>:11211"


Note: < server > is the memcached IP or URL which you can find by clicking Info button for the memcached node in your environment.

  1. Save the changes, restart the Apache node, and your app will start using memcached to store the PHP sessions.

This was just a simple example. If one of the instances fails, the users who were on that instance get automatically switched to the other instance in this cluster. Thanks to memcached, end users never notice any change. Now you can deploy a high-availability PHP cluster in a few minutes in the ServInt Jelastic cloud.

Find out more about ServInt solutions

Starting at $69

  1. High availability for your #PHP and #Java apps requires redundancy and scalability with clustering in the #cloud.
  2. I'm a complete noob with all of this. I just wanted to know how well SSL is handled via NGINX and this clustered/balanced solution ?
  3. RT @servint: What's the easiest way to extend #PHP across multiple nodes? Clusters with ServInt's #Jelastic #cloud.
  4. What's the easiest way to extend #PHP across multiple nodes? Clusters with ServInt's #Jelastic #cloud.
  5. PHP session clustering in the #cloud, in this week's #TechBench.
Start the conversation

Dominik Poetek

Dominik Poetek

Head of Escalated Support, ServInt

Dominik Poetek is the Head of Escalation Support at ServInt. He has been a part of ServInt’s Managed Services Team since April of 2004.

  • The New York Times
  • The Hill
  • Bloomberg
  • The Seattle Times
  • Computer World
  • Ars Technica