MilesWeb Cloud hosting is powered to provide session replication between web server instances (TomEE 1.7, Tomcat 6/7, GlassFish 3, Jetty 6).
Session replication is a mechanism used to replicate the data that is stored in a session between different instances, which must be part of the same cluster. When session replication is enabled in a cluster environment, the entire session data is copied to a replicated instance. The session replication operation does not copy attributes that cannot be serialized in a session and any instance-specific data. Session replication delivers high reliability, scalability, and seamless failover capabilities.
In the MilesWeb cloud, the process of replication takes place utilizing multicast. In such a way we have the next benefits:
How to Adjust Session Replication
To use replication in MilesWeb Cloud, you have to get through the next steps:
1. Log into the cloud panel.
2. Click Create Environment.
3. Pick application servers that you need.
4. Turn on "High-availability" as shown on the image below:
During the creation of the environment, all actions are displayed in the task manager:
Once the creation of environment is done, you can see that replication is turned on as shown on the image below:
When the user performs his first request to the web application balancer (NGINX), it redirects the request to one of the clusters. During the session, the user continues to work with that same instance of the application server (so called Sticky Sessions.) Certainly, the other instance in the cluster is also working to serve its half.
If one of the instances fails, the users who were in that instance are automatically switched to the other instance of that cluster. With this replication, the other instance already has all instances of the failed instance, so end users never notice changes. In the highly improbable case that both instances in a cluster fail, users are redirected to another cluster.
Also, users can turn on/off replication during changing topology of the environment that has been already created.
MilesWeb cloud solution provides Session Replication in between instances of web servers with a help of multicast. But you can also use Session Replication using Memcached.
To use memcached for session replication follow these instructions.
1. First, you will need to download .jar file of the Memcached session manager. For the example, we have used memcached-session-manager-1.6.2.
You will also need to download memcached-session-manager-tc7-1.6.2.jar, spymemcached-2.8.4.jar, msm-kryo-serializer-1.6.1.jar, kryo-1.03.jar, reflectasm-0.9.jar, kryo-serializers.jar, joda-time.jar and minlog-1.2.jar.
2. Click Config for Tomcat.
3. In the opened window, choose lib folder and upload the .jar file that you have just downloaded.
4. Choose server folder and open context.xml file.
5. Update context.xml so that it contains the Manager configuration for the memcached-session-manager, like this:
<Context path="" docBase="ROOT"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:host:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" copyCollectionsForSerialization="false" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" customConverter="de.javakaffee.web.msm.serializer.kryo.JodaDateTimeRegistration" /> </Context>
You can simply use any other serialization strategy according to your requirements, in this example we use Kryo. This is an extremely fast binary serialization library.
6. In the string memcachedNodes, add your memcached host and default port (11211). In our case we have:
So as to get your memcached host, you simply need to add the 'memcached-' prefix to your environment hostname (which is displayed in a string just below the name of your environment).
7. Save the changes and Restart your server node (in our example, Tomcat).
Now you have a cluster with all the advantages of Memcached and high availability.
PHP Sessions Clustering
You can achieve the high availability for your PHP application by implementing PHP sessions clustering in the cloud. To perfectly handle the application server failover, we have created an environment with Memcached, multiple Apache servers, and a NGINX load balancer:
1. Log into MilesWeb Cloud Manager.
2. Click on Create Environment.
3. In the Environment Topology window, you must choose two or more servers that you want to use (for example, two instances of Apache) and the Memcached node. Type the name of the environment and click Create.
The Memcached node is utilized as a distributed caching engine in the environment with multiple nodes. In our case, it provides a Web session with sticky sessions that run on multiple application servers. If a server fails, the sessions are stored for backup on the Memcached node. Other servers can retrieve Memcached sessions and begin serving the session.
When an individual session request is completed, the session is sent to the Memcached node for backup. This session remains available on the original application server and can be used to resolve the following request. After the second completion of a request, the session is updated in Memcached.
If the primary server fails, the next request is routed to another application server. The newly requested server is requested for the session that it does not know. This new server will then find the required session on the Memcached node. It is identified according to the identifier that was attached to the SessionID during the formulation of the session. As a result, the server can fetch the session from the Memcached node.
When the server responds to the request, it also updates the session on the Memcached node. Therefore, there is no application disruption caused by the primary server error - the failover is handled correctly.
Simultaneously, the NGINX load balancer distributes traffic through the cluster that contains HTTP resources. You can check load balancing in the MilesWeb cloud by using different load balancing tools.
1. Navigate to your environment in the MilesWeb cloud dashboard and click Config button for Apache.
2. In the opened tab go to etc > php.ini
3. Add the next line to Dynamic Extensions:
4. Make the changes in the [Session] block:
session.save_handler = memcached
session.save_path = "> server <:11211"
Note : You can find > server < states for memcached IP or URL by clicking Info button for the memcached node in your environment.
5. Save the changes and restart the Apache node.
You are done! Now if one of the instances fails, the users get automatically switched to the other instance in this cluster and never notice any change.