Session Replication via Memcached
If you didn’t know, Jelastic provides Session Replication between instances of web-servers with a the help of multicast. As we mentioned, this boils down to properly setting up session replication between Tomcat, GlassFish and Jetty web servers in Jelastic and configuring load balancer redirecting requests to them. You can also use Session Replication via Memcached.
The main purpose of Memcached is to provide an easy to use distributed caching engine in a multinode environment. But, imagine that you have a web application with sticky sessions running on several app servers and want to have some kind of session failover. You want to have a scalable solution for that – just add more servers to handle an increasing number of sessions. This can be processed by sessions that are stored for backup in Memcached node: if a one server dies all the others will take over the work of the dead one and fetch the sessions from Memcached and serve this session from thereon.
The memcached session manager and Jelastic will help you to implement this. The memcached session manager installed in a server holds all sessions in its own jvm. Additionally, after a request was finished, the session is sent to a memcached node for backup. When the next request for this session has to be served, the session is available and can be used. After the second request is finished the session is updated in the memcached node. Now lets imagine the server dies. The next request will be routed to another one. This application server is asked for a session it does not know. It will now lookup the session in the memcached node (based on an id that was appended to the sessionId when the session was created). It will fetch the session from memcached and store the session in its own jvm: it is responsible for that session from now on. After the server answers this request it also updates the session in the memcached node. So the server’s failover is handled completely.
To use memcached for session replication follow the given instruction:
Create the environment
1. Go to jelastic.com and sign up if you haven’t done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.
2. Ask Jelastic to create a new environment.
3. In the Environment topology window choose two or more servers you want to use (for example, two instances of Tomcat 7) and Memcached node. Type the name of the environment (for example, memcachedreplication) and click Create.
In a minute your environment will be created.
Configure application server
1. Download .jar file of Memcached session manager. As the example we used memcached-session-manager-1.6.2. Also 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 files you’ve 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">
You can use any other serialization strategy according to your needs, in our case we use Kryo, an extremely fast binary serialization library.
6. In the string memcachedNodes add your memcached host and default port(11211). In our case we have:
To get your memcached host click Info button for Memcached node in your environment.
7. In the opened window you’ll find the host. Copy it and add to the memcachedNodes string.
8. Save the changes and restart your server node (in our case Tomcat).
That’s all. Now your have a high available cluster with all the advantages of Memcached. Stay tuned for even more exiting stuff!