AWS Elastic Beanstalk and DynamoDB Session manager for Tomcat

The autoscaling of AWS Elastic Beanstalk is a great tool. You can configure so many params for trigger new instances. What we can do with the Persistence???

Step by Step…

By default, the session is stored in tomcat which create the session.
You can configure sticky sessions to get a client connected to the same tomcat and keep the session for future requests of that client.

In the case of a growing number of instance everything will work it out. Instead of a decreased number of instances, current sessions will be reassigned to instance that gonna stay alive. The current session stored in a shutting down tomcat will be lost.

How to keep sessions between different tomcats

Cluster

You can configure a cluster. Ok, first of all each web app has to know the others web app, easy for start new instances, but old ones does not know the new instances. Besides, cluster has a poor scalable performance.

Common session storage

Instead store session in the tomcat that create it, you can store the session in a common storage service, like db or cache

This is our solution for AWS Elastic Beanstalk

Session persistence in AWS

Amazon crew has development a library to provide a session manager for Tomcat 7 that persists session data in Amazon DynamoDB.
Source github.com/aws/aws-dynamodb-session-tomcat

Configuration for Tomcat

  1. Download from aws-dynamodb-session-tomcat releases the AmazonDynamoDBSessionManagerForTomcat-1.0.1.jar and copy to the lib directory of your Tomcat installation
  2. Edit context.xml to use the .jar with code:

Configuration for Elastic Beanstalk

  1. Download from aws-dynamodb-session-tomcat releases the AmazonDynamoDBSessionManagerForTomcat-1.0.1.jar and copy into .ebextensions directory of your WAR
  2. Edit context.xml with the code below and copy into .ebextensions directory of your WAR
  3. In the .ebextensions directory (Remember it must be inside /WEB-INF directory, /web-app for grails applications), create a .config file to write the command to execute the replacement of context.xml and copy the jar inside Beanstalk instances.

Now, just upload and deploy the war with the changes. Try login and go into AWS Console->Services->DynamoDB, look for a table with name Tomcat_SessionState and voilà! You have affinity and persistence!
Sources:


Advertisements

One thought on “AWS Elastic Beanstalk and DynamoDB Session manager for Tomcat

  1. Pingback: [AWS][Beanstalk] DynamoDBとの連携 | halclan.i.e.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s