Offload HTTP Sessions with Spring Session and Redis


Applications running in Cloud Foundry should offload the sessions to a Redis service. The applications should be stateless in nature, and any crash of the an application instance, should not impact the other application instances and user workflows.

Configure the service and bind to the application

By default, the Tomcat instance is configured to store all Sessions and their data in memory. Under certain circumstances it my be appropriate to persist the Sessions and their data to a repository. When this is the case (small amounts of data that should survive the failure of any individual instance), the buildpack can automatically configure Tomcat to do so by binding an appropriate service.

The Pivotal Platform GO Router uses the jsessionid plus a vcap_id to establish sticky sessions. Session replication breaks with sticky sessions at the GO router which is one of the reasons Spring Session is used. By default Spring Session creates a custom SESSION cookie to house the applications HTTP session.

See: Note: CF does not persist or replicate HTTP session data across app instances. If an app instance crashes or is stopped, session data for that instance is lost. If you require session data to persist across crashed or stopped instances, or to be shared by all instances of an app, store session data in a CF marketplace service that offers data persistence.



NOTE: This recipe was customized for Spring 3.2.18 and XML Configuration. It is highly recommend to bootify your application and leverage the Spring Boot Starter modules to help stay current with Spring versions.

Pivotal Platform and Redis Service Creation

To enable Redis-based session replication, simply bind a Redis service containing a name, label, or tag that has session-replication as a substring.

NOTE: After initial setup and testing of the redis session functionality, it is recommended to add your service binding to the Pivotal Platform manifest.yml file.

> cf create-service p-redis <plan-name> session-replication

> cf bind-service <app-name> session-replication

> cf restage <app-name>

Manual Definition for session replication with Redis

Since the code uses the Tomcat HTTPSession, we need to update to SpringSession as SpringSession can handle the objects marshalling/unmarshalling better than Tomcat HttpSession, we need to add the following dependencies in our web modules.

NOTE: USE SPRING SESSION from the beginning of your session replication adventure. CF has built-in session management capabilities but it has limitations. ]


Define the session configuration in your web.xml

NOTE: For the session replication to work with spring security, you need to place the spring session filter above spring security filter.

NOTE: For the session replication to work with struts, you need to place the spring session filter above the struts filter.


Define the following in your Spring application-context.xml:

  <bean class=""/>
  <bean class="" />
  <util:constant static-field=""/>