Autoscaling in AWS Part 2: Autoscale workers in ECS

Santi Muñoz
Signaturit Tech Blog
3 min readSep 28, 2018

In this second part we will implement the autoscaling for our workers. They are also deployed in ECS as a services.

If you missed the first part about austoscale HTTP services, you can check it out here!

Scale Out workers

Regarding the workers, the scale out process is pretty much the same as the HTTP services, published in the Part 1 of this series, but instead of using the TargetResponseTime we will use the ApproximateNumberOfMessagesVisible metric from SQS.

In our architecture there is a particularity with the workers, one worker is consuming from multiple queues, so we will scale out when one of the queues breach the threshold of 600 messages in queue.

Scale In workers

Due to the workers are consuming from multiple queues at the same time, we can not use a single metric of a queue to decide if the worker have to be scaled in. If one of the queues is empty doesn’t mean that ALL the queues are empty, so the rule to scale in the workers would be to check if ALL the queues are empty.

To execute this we will create a lambda that will evaluate all the queues periodically, and if all are OK, it will publish a custom metric to indicate that the worker needs scale in.

In our case we have an array of the alarms for the queues, the method below iterate over all of them, and check that all are in state OK.

Once decided that the worker can scale in, we will publish a custom metric in Cloudwatch to indicate it. This custom metric is called <worker name>-needs-scaleIn.

For the service in ECS we also have to create the ScalePolicy and the alarm that triggers this policy.

Summary

If your service needs multiple metrics to evaluate the scalability, you have to implement a custom service and metric to handle it.

Another possible implementation would be to use the Step Functions from AWS, but we preferred in this case to use a simple lambda to handle it.

In the next post we will implement the autoscaling for the machines of the ECS clusters, surely the most challenging part!

About Signaturit

Signaturit is a trust service provider that offers innovative solutions in the field of electronic signatures (eSignatures), certified registered delivery (eDelivery) and electronic identification (EID).

Open Positions

We are always looking for talented people who share our vision and want to join our international team in sunny Barcelona :) Be a SignaBuddy > jobs

--

--