Open Gitter Chat

Clustering

What is Session Clustering?

Web servers and applications servers can easily be made to “Scale Out” to handle huge loads by adding things like a load balancer. However, web applications need to hold the “state” of a given user session. First and foremost, the user identity and authentication state–is the user logged in? Is the “user” accesing it doing so via API? Is there a security token that represents that user?

So a session lives on the machine where it was first created. This forces all user traffic from that user to go through that specific machine. This is actually the most reasonable thing to do as it improves performance, but over long periods of time such as days and weeks, it may make less sense to bind a user to a machine. Many modern web applications have a “keep me logged in” checkbox that makes this possible. Also, there may be instances where a machine needs to be restarted or spontaneously crashes. You do not want to suddenly destroy all of the user’s work or transactions in such cases.

How does Hazelcast Help?

Hazelcast is easily able to handle this type of case with in-memory performance, linear scalability as you add new nodes and reliability. This also takes advantage of Hazelcast’s easy deployment footprint, as the Hazelcast is a very small library of 2.6MB and can easily be embedded in every copy of your web application server or servelet engine such as Tomcat. Because of this deployment approach as well as Hazelcast’s ability to automatically discover and cluster with peers, Hazelcast provides drop-in session clustering ability for any Java enabled server. It requires no additional investment in hardware and elastically scales as you add app servers.

This is a great way to ensure that session information is maintained when you are clustering web servers. You can also use a similar pattern for managing user identities. Learn how easy it is to maintain session state across a set of servers here!

Easy Hazelcast Deployment

Say you have more than one web servers (A, B, C) with a load balancer in front of them. If server A goes down then your users on that server will be directed to one of the live servers (B or C) but their sessions will be lost! So we have to have all these sessions backed up somewhere if we don’t want to lose the sessions upon server crashes. Hazelcast allows you to cluster user http sessions automatically. The following are required for enabling Hazelcast Session Clustering:

  • Target application or web server should support Java 1.5+
  • Target application or web server should support Servlet 2.4+ spec
  • Session objects that needs to be clustered have to be Serializable

Here are the steps to setup Hazelcast Session Clustering:

  1. Put the hazelcast and hazelcast-wm jars in your WEB-INF/lib directory.
  2. Put the following xml into web.xml file. Make sure Hazelcast filter is placed before all the other filters if any; put it at the top for example.
    <filter>
    	<filter-name>hazelcast-filter</filter-name>
    	<filter-class>com.hazelcast.web.WebFilter</filter-class>
    		<!--
                Name of the distributed map storing
                your web session objects
            -->
            <init-param>
                <param-name>map-name</param-name>
                <param-value>my-sessions</param-value>
            </init-param>
            <!-- How is your load-balancer configured? stick-session means all requests of a session is routed to the node where the session is first created. This is excellent for performance. If sticky-session is set to false, when a session is updated on a node, entry for this session on all other nodes is invalidated. You have to know how your load-balancer is configured before setting this parameter. Default is true. -->
            <init-param>
                <param-name>sticky-session</param-name>
                <param-value>true</param-value>
            </init-param>
            <!--
                Are you debugging? Default is false.
            -->
            <init-param>
                <param-name>debug</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>hazelcast-filter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>INCLUDE</dispatcher>
            <dispatcher>REQUEST</dispatcher>
        </filter-mapping>
    
        <listener>
            <listener-class>com.hazelcast.web.SessionListener</listener-class>
        </listener>
  3. Package and deploy your war file as you would normally do.

It is that easy! All http requests will go through Hazelcast WebFilter and it will put the session objects into Hazelcast distributed map if needed.

Hazelcast.org

Main Menu