Failing-over
web session caching is problematic when you run multiple application servers. It
is not uncommon for web applications to run in a cluster to distribute the load
of high volume of web requests. But what if one of the application servers
crashes? The load balancer will just route the web request to another available
application server, but all of user’s session data is lost. In simple words,
you may be filling your shopping cart with your favorite items, but if the
application server serving your request crashes, you will end up with an empty
cart.
So, when an application server fails, web requests get routed to some
other application server that simply fetches the web session from GridGain cache.
This
process happens in the background and is so seamless that it does not affect
the users’ experience. Not only that, GridGain also ensures fault tolerance by either
replicating or partitioning the data, which is easily configurable, across all
grid nodes in the cluster. And so, no session data is lost.
Moreover,
a web request can now be sent to any active application server, that can access
the session data from GridGain cluster, and so, you may choose to turn off the Sticky Connections support of the load
balancer.
With just a few simple steps you can enable web
sessions caching with GridGain in your application. All you need to do is:
1. Download GridGain and add the following jars to your application’s classpath:
· gridgain.jar
· gridgain-web.jar
· gridgain-log4j.jar
· gridgain-spring.jar
Or, if you have a Maven based project, add the following to
your application's pom.xml
<dependency>
<groupId>org.gridgain</groupId>
<artifactId>gridgain-fabric</artifactId>
<version> ${gridgain.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.gridgain</groupId>
<artifactId>gridgain-web</artifactId>
<version> ${gridgain.version}</version>
</dependency>
<dependency>
<groupId>org.gridgain</groupId>
<artifactId>gridgain-log4j</artifactId>
<version>${gridgain.version}</version>
</dependency>
Make sure to replace ${gridgain.version}with
actual GridGain version.
2. Configure
GridGain cache in either PARTITIONED mode
<bean
class="org.gridgain.grid.cache.GridCacheConfiguration">
<!-- Cache name. -->
<property name="name"
value="partitioned"/>
<!-- Cache mode. -->
<property name="cacheMode"
value="PARTITIONED"/>
<property name="backups"
value="1"/>
...
</bean>
or REPLICATED mode
<bean
class="org.gridgain.grid.cache.GridCacheConfiguration">
<!-- Cache name. -->
<property name="name"
value="replicated"/>
<!-- Cache mode. -->
<property name="cacheMode"
value="REPLICATED"/>
...
</bean>
You can also choose
to use the default cache configuration, specified in GRIDGAIN_HOME/config/default-config.xml, shipped with GridGain installation.
3. Declare a
context listener in the application’s web.xml.
...
<listener>
<listener-class>org.gridgain.grid.startup.servlet.GridServletContextListenerStartup</listener-class>
</listener>
<filter>
<filter-name>GridGainWebSessionsFilter</filter-name>
<filter-class>org.gridgain.grid.cache.websession.GridWebSessionFilter</filter-class>
</filter>
<!-- You can also specify a custom URL pattern.
-->
<filter-mapping>
<filter-name>GridGainWebSessionsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Specify GridGain configuration (relative to
META-INF folder or GRIDGAIN_HOME). -->
<context-param>
<param-name>GridGainConfigurationFilePath</param-name>
<param-value>config/default-config.xml </param-value>
</context-param>
<!-- Specify the name of GridGain cache for web
sessions. -->
<context-param>
<param-name>GridGainWebSessionsCacheName</param-name>
<param-value>partitioned</param-value>
</context-param>
...
4. Optional – Set eviction policy
for stale web sessions data lying in cache.
<bean
class="org.gridgain.grid.cache.GridCacheConfiguration">
<!--
Cache name. -->
<property name="name" value="session-cache"/>
<!--
Set up LRU eviction policy with 10000 sessions limit. -->
<property name="evictionPolicy">
<bean
class="org.gridgain.grid.cache.eviction.lru.GridCacheLruEvictionPolicy">
<property name="maxSize" value="10000"/>
</bean>
</property>
...
</bean>
Conclusion
The
main advantage of GridGain web sessions caching is that it ensures that the
user session data is always available no matter which application server the
user’s web request is routed to. Sticky
Connections support is also not required since the web sessions’ data is
now available to all application servers.
Another
advantage is that in GridGain, data is always stored in memory vs. maintaining
a copy of sessions’ data on disk. Therefore, the performance of your
application does not get compromised while recovering the session data owned by
the failed application server.