Fargate vs EC2 launch type in AWS - Performance Comparison
We want to understand how the time it takes to run tasks changes when using Fargate or EC2 launch types in AWS ECS.
Specifically, how does the number of tasks affect the time and operations involved?
Analyze the time complexity of launching multiple ECS tasks using Fargate and EC2 launch types.
// Using Fargate launch type
for (let i = 0; i < n; i++) {
ecs.runTask({
launchType: 'FARGATE',
taskDefinition: 'myTaskDef',
networkConfiguration: {...}
});
}
// Using EC2 launch type
for (let i = 0; i < n; i++) {
ecs.runTask({
launchType: 'EC2',
taskDefinition: 'myTaskDef',
cluster: 'myCluster'
});
}
This sequence runs n tasks either on Fargate or EC2, one task per API call.
Look at what repeats as we increase tasks.
- Primary operation: The ecs.runTask API call to start each task.
- How many times: Exactly n times, once per task.
- Additional difference: EC2 requires managing container instances, which involves extra background operations but not per task API calls.
As the number of tasks n grows, the number of API calls grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 runTask calls |
| 100 | 100 runTask calls |
| 1000 | 1000 runTask calls |
Pattern observation: The number of API calls grows linearly with the number of tasks.
Time Complexity: O(n)
This means the time and number of operations increase directly in proportion to the number of tasks you run.
[X] Wrong: "Launching tasks on Fargate is faster because it uses fewer API calls than EC2."
[OK] Correct: Both launch types require one API call per task; the difference is in how resources are managed behind the scenes, not in the number of calls.
Understanding how task launch scales helps you design systems that handle growth smoothly and explain your choices clearly in discussions.
What if we batch multiple tasks in a single API call? How would the time complexity change?