Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 266
Chris Hundt Avatar asked Oct 22 '25 09:10

Chris Hundt


2 Answers

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.

like image 198
Soccergods Avatar answered Oct 27 '25 03:10

Soccergods


I realize now that there was an incorrect premise in my question:

Fargate has a task size and the combined hard limits ("memory" option) of a task's containers cannot exceed the task size's memory.

In fact, the combined soft limits ("memoryReservation" option) cannot exceed task memory. The combined hard limits can. So on Fargate you could schedule multiple "bursty" containers in a single task, and a single container could use up to the task's entire available memory before getting killed. This is somewhat similar to what you can achieve on EC2 instances using single-container tasks and memoryReservation.

You don't need to use memoryReservation to accomplish it on Fargate, though (you can just not set any memory limits or reservations beyond the task size). So the question still remains, why use that setting on Fargate? I think the best answer is probably the one provided by MasterFowl: to hint to the kernel not to allow one of your containers to take up too much of the task's memory.

like image 29
Chris Hundt Avatar answered Oct 27 '25 03:10

Chris Hundt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!