Need help with scaling dockers on ECS

I am trying to run docker containers on ECS. With 5-10 requests i am getting response within my acceptable timeframe.
But if there are 100s of requests aws i taking minutes to scale new instances and meanwhile those requests are being forwarded to the already running tasks. Due to this tasks are failing bcoz of memory error.
How do i scale ECS correctly so that every request is processed. Where to route requests untill new tasks are started ?

Assuming you are talking of a web based application behind an ALB, you can use the builtin scaling on a per request per target basis (docs)

If you are dealing with backend tasks, such as jobs coming from SQS, you can simply scale based on the queue depth (example with compose-x)

If you want to do the same thing based on other metrics, the same logic applies as for SQS but a different metric source.

i have DL models on the container. So it takes 4-5 mins to process each request. I am using ALB currently, but it is routing multiple requests to the running ones and in the meanwhile spinning new tasks which are taking 4-5 to startup. With multiple requests to the same task i am getting memory error.

currently using ALB with 1 request per target as a policy, still facing issue

If the traffic trend can be predicted, you can use ALB’s predictive scaling to scale before hand.

For sudden burts, there is no other option than to keep some buffer containers/nodes for handling such spikes

To prevent excess requests, you can have an intermediate service like kong which can rate limit requests to ensure existing containers don’t die because of memory limits. Any excess requests can be served via some cache or returned a fix response

As already mentioned, you can use Predictive Autoscaling if your access patterns are fairly consistent (predictable).

The reason you have these delays is because you have servers which need to scale up prior to launching more containers. You can leverage AWS Fargate to remove the server management requirement from your ECS environment. With Fargate, instead of ECS launching containers in EC2 instances which you maintain, they’re launched within a fleet of server capacity maintained by AWS. Since that fleet is always scaled up to handle a lot of capacity, there’s no “cold start” related to waiting for EC2 instance launches.

That said, if you have a large image it can take some time to download to the Fargate instance. So there may still be delays. There’s an open issue for caching on the public containers roadmap.

That’s fair. I just wanted to make sure the OP was aware of options. Fargate-launched containers are almost certainly going to have far less “cold start” time than spinning up an EC2 + launching a container on it. But you’re right, my language might have implied a “universal silver bullet” answer, which would not be correct. One of the nice things about AWS: the OP now has some ideas about approaches which can be cheaply and easily tested, measured, and compared before committing to anything.

what’s your container size and startup time? large containers take a while to download and if youre doing something like bundle install or npm build in your CMD or ENTRYPOINT then it’s going to take a very long time

Yep for sure! I’ve just run into the image downloading thing myself and gotten surprised by it.

i have some deep learning models on the container. Entrypoint is a simple flask app which loads the models at start. It took some 1-2 mins to start the container.

Thanks i can try out Fargate, but can i use GPU with it ?
I have some DL models to run on the container, so GPU instances would help a lot.
I didn’t knew of predictive autoscaling. Thanks :+1:. I’ll try it out .

Okay, thanks will try predictive scaling :+1:
Putting an SQS with cloudwatch can be used to control request right ? Or there can be issues with it.

A queue delay will adversely affect response time. Though this will save ur containers

A caching layer can still reduce overload by a big margin

Ah, yeah to the best of my knowledge, Fargate does not currently support GPUs. So, you may want to adjust how you’re going about handling your cluster scaling. Predictive Autoscaling may be part of this. I’d also be sure that you’re managing EC2 costs through Reserved Instance or Savings Plans. It’s a little easier to justify getting aggressive with scaling when you’re running under discounted pricing.

Depending on the details of how your application works, the AWS Batch service supports GPU-based instances. Good luck!

Thanks for the detailed explanation :raising_hands:

Have no idea of caching layer but will explore it. Thanks

With response times in minutes, I would recommend moving towards a more asynchronous flow. Put incoming requests on a queue, and put workers behind that queue with autoscaling on queue depth. Return results by pushing them to the user, or implementing a polling process on the requesting side.

This doesn’t directly address your problem, but…

You should look into load shedding so that your running servers don’t crash. Essentially you only process so many requests at a time and queue up the rest. If the queue gets too long, you start rejecting requests. That way, even though some requests are failing, your servers will remain healthy and you can continue serve some requests successfully.

AWS wrote about using this technique to build resilient systems: https://aws.amazon.com/builders-library/using-load-shedding-to-avoid-overload/

I built a Dockerized reverse proxy with load shedding. I use it in my own ECS clusters. https://github.com/luhn/spillway

Thanks, will check it out :+1: