Recently, the Java Buildpack added support for session replication using Redis. This support complements the existing session affinity (i.e. sticky sessions) that Cloud Foundry already supports. So now, in addition to having sessions routed to the instance they were created on, sessions will also survive the loss of that instance. The Java Buildpack will auto-configure Redis-based Session Replication for you when it notices that you’ve bound to a specially identified Redis service.
By default, the Tomcat instance provided by the Java Buildpack is configured to store all sessions and their data in memory. To make the instance use Redis-based Session Replication instead, bind a Redis service containing a
tag that has
session-replication as a substring and the Java Buildpack will do the rest.
restageing your application will cause the required dependencies to be downloaded and configured.
$> cf restage cfrost-session-application
-----> Java Buildpack Version: 41ccef3 | https://github.com/cloudfoundry/java-buildpack.git#41ccef3
-----> Downloading Open Jdk JRE 1.7.0_65 from http://download.run.pivotal.io/openjdk/lucid/x86_64/openjdk-1.7.0_65.tar.gz (found in cache)
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.0s)
-----> Downloading Spring Auto Reconfiguration 1.4.0_RELEASE from http://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.4.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Instance 7.0.54 from http://download.run.pivotal.io/tomcat/tomcat-7.0.54.tar.gz (found in cache)
Expanding Tomcat to .java-buildpack/tomcat (0.1s)
-----> Downloading Tomcat Lifecycle Support 2.2.0_RELEASE from http://download.run.pivotal.io/tomcat-lifecycle-support/tomcat-lifecycle-support-2.2.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Logging Support 2.2.0_RELEASE from http://download.run.pivotal.io/tomcat-logging-support/tomcat-logging-support-2.2.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Access Logging Support 2.2.0_RELEASE from http://download.run.pivotal.io/tomcat-access-logging-support/tomcat-access-logging-support-2.2.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Redis Store 1.2.0_RELEASE from http://download.run.pivotal.io/redis-store/redis-store-1.2.0_RELEASE.jar (0.0s)
Adding Redis-based Session Replication
-----> Uploading droplet (45M)
1 of 1 instances running
The console output shows that the Tomcat Redis Store is downloaded because your application is bound to a Redis service. Redis-based Session Replication is then added to the Tomcat instance because of the service’s name. You can verify the session persistence directly by exercising your application from a couple of different browsers and then using the
redis-cli tool to inspect Redis.
$> redis-cli keys *
The Redis keys are the ids of the stored sessions. These sessions can be retrieved by any instance of your application. This means that if an instance of your application crashes or is restarted, any other live instance can pick up the session and the user won’t have to start again. You can try this for yourself with this demo application and a full walk-through in Ben Hale’s CF Summit 2014 talk at 9 minutes in.
About the Author