Paul Taylor
Paul Taylor

Reputation: 13120

Auto Scaling not working properly with Tomcat in Elastic Beanstalk

I created an Autoscaled Elastic Beanstalk application using Tomcat (java based) to provide a webservice.

As part of first-time initialization I have to copy and uncompress some large files from S3 to the underlying EC2 instance, ( this step is not required if the instance is restarted and already has the data files). This is all done within the servlets init() method and works perfectly within a non scaled environment - but it is slow - taking about 30 minutes.

But when I created a autoscaled environment i noticed two problems:

  1. It actually started three EC2 instances even though not a single call made to the service, I assume whilst monitoring the first instance to start it wasn't responding because it was busy in the init() method and therefore it started another instance and so on.

  2. It indicates that the services were ready even though they were busy in their init() methods and not yet ready to accept requests.

Surely Elastic Beanstalk should wait for the init() method of the first instance to complete BEFORE it indicates as ready and before it monitors it as busy and considers starting new instances.

Then I left it for twenty minutes and because there was no activity two of the three instances were then stopped. So I thought okay I can work round this the first time I deploy I can just wait for a bit and it will sort itself out, and then when the server is genuine busy I already have an extra two instances all ready to go with the data preloaded so they will start quickly. But I looked again and found those two instances have now been terminated and so think the problem will be that if EB decides to start a new instance because instance1 is genuinely busy there will be a period of time where instance2 is not really ready but is accepting requests, therefore user requests may succeed or may fail depending on whether they are redirected to instance1 (works) or instance2 (fails).

Am I correct in my assumptions, why doesn't EB respect init() method and how can I work round this problem ?

Upvotes: 1

Views: 1200

Answers (1)

Julio Faerman
Julio Faerman

Reputation: 13501

Elastic Beanstalk is mostly unaware of the application server internals, including servlet context listeners. Beanstalk will health check your environment resources, and, if your instances are not responding, they may be replaced according to your auto-scaling configuration. This is probably the reason why you see more instances coming up without traffic.

The application health monitoring performed by Elastic Beanstalk is described here:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.healthstatus.html

It is probably a better idea to setup your instances using .config files than context listener init(), see:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.customami.html

Upvotes: 2

Related Questions