Serverless Infrastructure Autoscaling

Large-scale compute clusters are expensive, so it is important to use them well. Utilization and efficiency can be increased by running a mix of workloads on the same machines: CPU- and memory-intensive jobs, small and large ones, and a mix of offline and low-latency jobs – ones that serve end-user requests or provide infrastructure services such as storage, naming or locking.

The challenge of scaling containers

The ECS topology is built on clusters, where each cluster has services (which can be referred to as applications), and services run tasks. Each task has a task definition which tells the scheduler how many resources the task requires.

For example, if a cluster runs 10 machines of c3.large (2 vCPUs and 3.8 GiB of RAM) and 10 machines of c4.xlarge (4 vCPUs and 7.5 GiB of RAM), the total vCPUs is 60*1024 = 61,440 CPU Units and the total RAM is 113 GiB.

The issue here is that if a single task requires more RAM than the individual instance has, it can’t be scheduled. In the example above, a task with 16 GiB of RAM won’t start, despite the total available RAM being 113 GiB. Ocean matches the task with the appropriate instance type and size while requiring zero overhead or management.

Ocean dynamically scales the cluster up and down to ensure there are always sufficient resources to run all tasks and at the same time maximizes resource allocation in the cluster. It does so by optimizing task placement across the cluster in a process we call Tetris Scaling, and by automatically managing headroom, a buffer of spare capacity (in terms of both memory and CPU) that ensures that when you want to quickly scale  more containers, you don’t have to wait for new VMs (i.e., instances) to be provisioned.

Scale Up Behavior

Ocean keeps track of tasks that cannot be scheduled and employs the following process for scaling up.

  1. Ocean continuously looks for unsatisfied ECS services, i.e., services for which the count of running tasks is lower than the desired task count.
  2. For those services, Ocean simulates placement of the desired tasks on the cluster’s current container instances, including instances that were just launched and not yet registered as container instances (for example, according to previous scale up activity).
  3. If the simulation results in no container instances that are able to host all pending tasks, Ocean will scale up for the remaining tasks. During the scale up, Ocean uses its launch specifications to consider all placement constraints.

When the process is completed, all tasks are scheduled and running. Ocean continues to monitor for unsatisfied ECS services, as described in Step 1 above, and will initiate additional scale up if it becomes necessary.

Headroom

Ocean provides the option to include a buffer of spare capacity (vCPU and memory resources) known as headroom. Headroom ensures that the cluster has the capacity to quickly scale more tasks without waiting for new container instances to be provisioned. The headroom capacity is kept across the cluster and as separate units in order to support scheduling of new pods. 

You can configure headroom in specific amounts of vCPU and memory or specify headroom as a percentage of the total CPU and memory.

Ocean optimally manages the headroom to provide the best possible cost/performance balance. However, headroom may also be manually configured to support any use case.

Placement Constraints

Ocean supports built-in and custom task placement constraints within the scaling logic. Task placement constraints give you the ability to control where tasks are scheduled, such as in a specific availability zone or on instances of a specific type. You can utilize the built-in ECS container attributes or create your own custom key-value attribute and add a constraint to place your tasks based on the desired attribute.

Ocean provides a custom placement restraint which enables you to control how specific tasks are scheduled. For example, if you have mission critical tasks that cannot run on spot instances, you can use the placement restraint “spotinst.io/container-instance-lifecycle” to schedule tasks only on on-demand instances. The format of the placement restraint is shown below:

"attribute:spotinst.io/container-instance-lifecycle==od"

In order to use this feature, you will need ecs:putAttributes permissions in your AWS IAM role (see Spot Policy for AWS).

Scale Down Behavior

Ocean monitors the cluster and runs bin-packing algorithms that simulate different permutations of task placement across the available container instances. A container instance is considered for scale down when:

  • All the running tasks on the particular instance are schedulable on other instances.
  • The instance’s removal won’t reduce the headroom below the target.
  • Ocean will prefer to downscale the least utilized instances first.

When an instance is chosen for scale-down it will be drained. Its running tasks are rescheduled on other instances, and the instance is then terminated.

Usage Notes: 
  • Ensure that permission for “ecs:stopTask” is included in your Spot policy. The stopTask action is required for the drain process and used to stop tasks that are still not drained after the draining timeout has passed.
  • Scale-Down actions are limited by default to 10% of the cluster size at a time. This parameter is configurable.
Scale Down Prevention

It is possible to mark a resource so that it will not be scaled down. You can use the tag spotinst.io/restrict-scale-down to prevent the scale down of a task or a service.

The following is an example of the tag that you add to the task definition or the service:

"tags": [
   {
     "key": "spotinst.io/restrict-scale-down",
     "value": "true"
   }
 ]

When you add a new tag in a task definition, you must create a new revision. Otherwise, the new tag will not be visible.

For more information about tagging in ECS, see Tagging Your Amazon EC2 Resources.

Daemon Tasks

Daemon tasks run on each instance or on a selected set of instances in an Amazon ECS cluster and can be used to provide common functionality, such as logging and monitoring. Ocean automatically identifies and accounts for Daemon Tasks when optimizing capacity allocation to make sure the launched instances have enough capacity for both the daemon services and the pending tasks. It also monitors for new container instances in the cluster and adds the Daemon Tasks to them. Ocean supports and considers Daemon services and tasks, both for scale down and scale up behavior.

Scale down: A Daemon task which was part of a scaled-down instance won’t initialize a launch of a new instance and will not be placed on a different container instance.

Scale up: In case there is a Daemon scheduling strategy configured to one of the cluster services, Elastigroup will consider all newly launched instances to have enough spare capacity available in order to run the Daemon task properly in addition to other pending tasks.

Customizing scaling configuration

Ocean manages the cluster capacity to ensure all tasks are running and that resources are utilized. If you wish to override the default configuration, you can customize the scaling configuration.

To customize the scaling configuration:

  1. Navigate to your Ocean cluster.
  2. Click Actions on the top right side of the screen to open the actions menu.
  3. Click Customize Scaling.