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
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.
Configuration for Tomcat
- Download from aws-dynamodb-session-tomcat releases the AmazonDynamoDBSessionManagerForTomcat-1.0.1.jar and copy to the lib directory of your Tomcat installation
- Edit context.xml to use the .jar with code:
Configuration for Elastic Beanstalk
- Download from aws-dynamodb-session-tomcat releases the AmazonDynamoDBSessionManagerForTomcat-1.0.1.jar and copy into .ebextensions directory of your WAR
- Edit context.xml with the code below and copy into .ebextensions directory of your WAR
- 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!
- AWS Application Management Blog – Manage Tomcat Session State with Amazon DynamoDB
- AWS Doc – Manage Tomcat Session State with Amazon DynamoDB
- AWS Blog Java Development – Amazon DynamoDB Session Manager for Apache Tomcat