Search code examples
amazon-ecsaws-fargate

What does memoryReservation actually do on ECS with Fargate?


ECS' Container Definitions allow you to specify a memoryReservation for each container:

The soft limit (in MiB) of memory to reserve for the container. When system memory is under contention, Docker attempts to keep the container memory to this soft limit; however, your container can consume more memory when needed, up to either the hard limit specified with the memory parameter (if applicable), or all of the available memory on the container instance, whichever comes first. This parameter maps to MemoryReservation in the Create a container section of the Docker Remote API and the --memory-reservation option to docker run...If you specify memoryReservation, then that value is subtracted from the available memory resources for the container instance on which the container is placed; otherwise, the value of memory is used. For example, if your container normally uses 128 MiB of memory, but occasionally bursts to 256 MiB of memory for short periods of time, you can set a memoryReservation of 128 MiB, and a memory hard limit of 300 MiB. This configuration would allow the container to only reserve 128 MiB of memory from the remaining resources on the container instance, but also allow the container to consume more memory resources when needed.

I believe this is implemented using cgroups' "soft limits."

So that means, on ECS+EC2, the purpose of this option is to allow you to schedule a group of tasks on an instance which, if they all used their maximum memory at once, would exceed the total memory available on the instance, but you are predicting that they will not do that.

But what about on ECS+Fargate? Fargate has a task size and the combined hard limits ("memory" option) of a task's containers cannot exceed the task size's memory. [This last part was wrong; see my clarifying answer.] You are billed based on the task size regardless of the memory limits you set. So what benefit could you possibly get by setting a memoryReservation below your hard limit?

The only possibilities I can think of are:

  1. memoryReservation instructs AWS to pack your containers onto a smaller number of underlying VMs, risking performance issues at no benefit to you (since the price is the same).
  2. AWS just ignores memoryReservation for Fargate.

Have I missed any possibilities? If not , does Amazon say which one of the two it is?


Solution

  • What about the case where you have multiple containers in a single task. You might want to reserve some amount of memory for one container. Also you might have a case where different containers require a lot of memory at the same time and you don't want any of them to take too much memory and stall out the rest of the containers.