Session Replication via Memcached

Session Replication via Memcached

The platform can replicate web-server sessions through multicast, but Memcached can also be used as a shared session store. In this example, multiple Tomcat instances use Memcached Session Manager so session data remains available across the application-server cluster.
Application Servers Use two or more application-server instances, such as Tomcat.
Shared Session Store Add a Memcached node and configure the application servers to back up sessions to it.

Create the Environment

1Open the topology wizard

Sign in to the platform dashboard and click Create environment.

2Add application servers and Memcached

Select two or more application-server instances. The source example uses two Tomcat servers together with a Memcached node.

3Name and create the environment

Enter the environment name and click Create. The source example uses memcachedreplication.

Environment wizard for Memcached session replication
Create an environment with multiple Tomcat instances and a Memcached node.

After provisioning completes, the environment contains the application-server layer, Memcached, and the platform load-balancing component.

Environment for Memcached session replication
The created environment with Memcached and multiple Tomcat instances.

Download Session Manager Libraries

The source tutorial uses Memcached Session Manager for Tomcat and several supporting libraries. Its example versions are:

  • memcached-session-manager-1.6.2.jar
  • 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
  • minlog-1.2.jar
Historical versions: The versions above come from the source example and are old. For a current deployment, select Memcached Session Manager and dependency versions that match the Tomcat and Java versions in your environment.

Upload Libraries to Tomcat

1Open Tomcat Config

Click Config for the Tomcat application-server layer.

Tomcat configuration button
Open the Tomcat configuration file manager.

2Upload the required JAR files

Open the lib directory and upload all Memcached Session Manager and dependency JAR files.

Upload libraries to Tomcat lib folder
Place the session-manager libraries in the Tomcat lib directory.

Configure context.xml

Open the Tomcat server configuration folder and edit context.xml. Add a Memcached Session Manager definition similar to the source example:

<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>
Configure Tomcat context XML for Memcached replication
Add the Memcached Session Manager configuration to Tomcat context.xml.
The source example uses Kryo serialization. Other serialization strategies can be used when supported by the selected Memcached Session Manager version and application requirements.

Set the Memcached Host

Update the memcachedNodes value with the Memcached hostname and the default Memcached port 11211.

The source example uses:

n1:memcached-memcachedreplication.jelastic.com:11211

In the source workflow, the Memcached hostname is formed by adding the memcached- prefix to the environment hostname displayed beneath the environment name.

Environment hostname used for Memcached node
Use the environment hostname to determine the Memcached host referenced by memcachedNodes.
Port: The source configuration connects to Memcached on its standard port, 11211.

Save and Restart Tomcat

Save the updated context.xml and restart the Tomcat node so the new libraries and session-manager configuration are loaded.

Result: the application-server instances can use Memcached as a shared session backup layer, helping preserve sessions when requests move between servers in the cluster.
For production use, verify library compatibility, session serialization, application failover behavior, and Memcached availability before relying on this configuration for high availability.

What’s next?