| Sign In/My Account | View Cart |
Session Replication in Tomcat 5 Clusters, Part 1
Pages: 1, 2
Prior to version 5, Tomcat server only supported sticky sessions (using the mod_jk module for load balancing purposes). If we needed session replication, we had to rely on third-party software such as JavaGroups to implement it. Tomcat 5 server comes with session replication capabilities. Similar to the clustering feature, session replication is enabled just by modifying the server.xml configuration file.
Martin Fowler talks about three session-state persistence patterns in his book Enterprise Patterns. These patterns are:
Tomcat supports these three session persistence types:
SimpleTcpCluster and SimpleTcpClusterManager classes that ship with the Tomcat 5 installation. These classes are in the package org.apache.catalina.cluster and are part of server/lib/catalina-cluster.jar.JDBCManager class. This class is in the org.apache.catalina.session.JDBCStore package, and is part of the file catalina.jar.PersistenceManager class. This class is in the org.apache.catalina.session.FileStore package and is part of catalina.jar.This section briefly explains the elements comprising Tomcat cluster and session replication.
This is the main element in the cluster. The class SimpleTcpCluster represents the cluster element. It creates the ClusterManager for all of the distributable web contexts using the specified manager class name in server.xml.
This class takes care of replicating the session data across all of the nodes in the cluster. The session replication happens for all those web applications that have the distributable tag specified in the web.xml file. The cluster manager is specified in server.xml with the managerClassName attribute of the Cluster element. The cluster manager code is designed to be a separate element in the cluster; all we have to do is write a session manager class that implements the ClusterManager interface. This gives us the flexibility of using a custom cluster manager without affecting other elements in the cluster.
There are two replication algorithms. SimpleTcpReplicationManager replicates the entire session each time, while DeltaManager only replicates session deltas.
The simple replication manager copies the entire session on each HTTP request. This is more useful when the sessions are small in size, and if we have code like:
HashMap map = session.getAttribute("map");
map.put("data","data");
Here, we don't need to specifically call the session.setAttribute() or removeAttribute methods to replicate the session changes. For each HTTP request, all of the attributes in the session are replicated. There is an attribute called useDirtyFlag that can be used to optimize the number of times a session is replicated. If this flag is set to true, we have to call setAttribute() method to get the session changes replicated. If it's set to false, the session is replicated after each request. SimpleTcpReplicationManager creates a ReplicatedSession to perform the session replication.
The delta manager is provided for pure performance reasons. It does one replication per request. It also invokes listeners, so if we call session.setAttribute(), the listeners on the other servers will be invoked. DeltaManager creates a DeltaSession to do the session replication.
The membership is established by all Tomcat instances sending broadcast messages on the same multicast IP and port. The broadcast message contains the IP address and TCP listen port of the server (the default IP address value is 228.0.0.4). If an instance has not received the message within a given time frame (specified by the mcastDropTime parameter in the cluster configuration), the member is considered dead. The element is represented by the McastService class.
The attributes starting with mcastXXX are for the membership multicast ping. The following table lists the attributes used for IP multicast server communication.
| Attribute | Description |
|---|---|
mcastAddr |
Multicast address (this has to be the same for all of the nodes) |
mcastPort |
Multicast port number (this also has to be the same for all of the nodes) |
mcastBindAddr |
IP address to bind the multicast socket to a specific address |
mcastTTL |
Multicast Time To Live (TTL) to limit the broadcast |
mcastSoTimeout |
Multicast read timeout (in milliseconds) |
mcastFrequency |
Time in between sending "I'm alive" heartbeats (in milliseconds) |
mcastDropTime |
Time before a node is considered dead (in milliseconds) |
This element is represented by the ReplicationTransmitter class. When a multicast broadcast message is received, the member is added to the cluster. Upon the next replication request, the sending instance will use the host and port information and establish a TCP socket. Using this socket, it sends over the serialized data. There are three different ways to handle session replication in Tomcat 5. These are asynchronous, synchronous, and pooled replication modes. The following section explains how these modes work and the scenarios where each should be used.
This cluster element is represented by the ReplicationListener class. The attributes in the cluster configuration that start with tcpXXX are for the actual TCP session replication. The following table shows the attributes used to configure socket-based server communication for server replication.
| Attribute | Description |
|---|---|
tcpThreadCount |
Number of threads to handle incoming replication requests |
tcpListenAddress |
IP address for TCP cluster requests. (If this is set to auto, the address becomes the value of InetAddress.getLocalHost().getHostAddress().) |
tcpListenPort |
The port number where the session replication is received from other cluster members. |
tcpSelectorTimeout |
Timeout (in milliseconds) |
The replication valve is used to determine which HTTP requests need to be replicated. Since we don't usually replicate the static content (such as HTML and JavaScript, stylesheets and image files), we can filter the static content using the replication valve element. The valve is used to find out when the request is completed and initiate the replication.
The deployer element can be used to deploy apps cluster-wide. Currently, the deployment only deploys/undeploys to working members in the cluster so no WARs are copied upon startup of a broken node. The deployer watches a directory (watchDir) for WAR files when watchEnabled="true". When a new WAR file is added, the WAR gets deployed to the local instance, and is then deployed to the other instances in the cluster. When a WAR file is deleted from the watchDir the WAR is undeployed locally and cluster-wide.
All of the elements in the Tomcat cluster architecture, and their hierarchy, are shown in Figure 1.
|
|
The following section briefly explains how the cluster nodes share the session information when a Tomcat server is started up or shut down. For more detailed explanation, refer to the Tomcat 5 Clustering documentation.
TC-01: First node in the cluster
TC-02: Second node in the cluster
Host object is created, a cluster object is associated with it. When the contexts are parsed, if distributable is specified in web.xml, Tomcat creates the session manager (SimpleTcpReplicationManager instead of StandardManager) for the web context. The cluster class will start up a membership service (an instance of Member) and a replication service.
distributable specified in web.xml.
ReplicationValve will intercept the request before the response is returned to the user. At this point, it finds that the session has been modified, and it uses TCP to replicate the session to TC-02.
mod_jk ports. So no requests make it to TC-01 until it has received the session state from TC-02.
In this article, I talked about session replication in a clustered environment, and some design considerations when creating J2EE applications with an in-memory session replication requirement. I also discussed the clustering elements in Tomcat 5 container that are specific to session replication. In part two of this series, we'll look at how to configure session replication in a Tomcat cluster using different session managers and replication modes.
Srini Penchikala is an information systems subject matter expert at Flagstar Bank.
Return to ONJava.com.
It seems to me that Tomcat only deals with the "Conversational" category.
Thanks