How to calculate resources reservation for ECS task
When you create a ECS Task you should specify how much CPU and memory (RAM) each Container or the whole Task needs. It’s essential to specify correct values here so scheduler can make better decisions about which instances to place Tasks on.
Ignoring this rule leads to the unstable work of the application itself and its neighbours (Tasks that are running on the same instance).
Resource types
CPU (measured in CPU units) and Memory (measured in megabytes) are each a resource type. A resource type has a base unit. CPU is specified in units of cores, and memory is specified in units of bytes.
CPU and Memory are collectively referred to as compute resources, or just resources. Compute resources are measurable quantities that can be requested, allocated, and consumed.
How to calculate resources reservation for your brand new app
- Specify resources you think your app will require. In this example we use 100 CPU units and 100Mb of reserved memory with 150Mb for memory hard limit. (Hint: generally it’s recommended to set hard memory limit as
1.5*X
from reserved memory) - Launch your app as ECS Service in cluster and put some workload on it
- Go to the “Metrics” tab on the ECS Service details page in AWS Console. (URL:
https://us-east-1.console.aws.amazon.com/ecs/home?region=us-west-1#/clusters/<cluster name>/services/<service name>/metrics
) - See “CPUUtilization” chart, you should look at the orange line there that sands for the average CPU utilization. Let’s imagine that you see that your service now uses 250% CPU. This means that you need to adjust reserved resources for your Task using the following formula:
(X / 100) * Y * 1.3
. For our case this means that we should reserve(250 / 100) * 100 * 1.3 = 325
CPU units.
* X — current CPU utilization (percentage)
* Y — value you specified in Task parameters
* 1.3 — buffer capacity you want to reserve for workload spikes (Note: 1.3 value may not fit everyone’s needs, it’s just a baseline) - Repeat the same process for “MemoryUtilization” chart using the same. Let’s imagine that you see that your service now uses 150% RAM. For our case this means that we should reserve
(150 / 100) * 100 * 1.3 = 195
megabytes of memory. - Now as we know memory reservation value we can calculate hard memory limit. If app overflows this value — it will be killed by Docker engine. Usually it’s recommended to set it as
1.5 * X
from memory reservation value. So in our case it will be195 * 1.5 = 292.5
megabytes. - Apply changes and deploy new version of task definition.
- Monitor app and repeat steps starting from #4 again if needed.