EC2 versus Fargate: performance comparison
Nowadays, Fargate is very popular. It solves such ECS with EC2 problems as:
- Scaling problem (with EC2 you should have ASG which will add additional nodes if not enough resource in ECS cluster. It’s quite tricky)
- Not efficient resource utilization (with EC2 you always need a free buffer in ECS cluster for fast tasks allocations)
- Regular AMI updates
- ECS agent disconnection
Since there is no good performance comparison for EC2 and Fargate we will run the performance test and analyze it.
Our goal is to understand if Fargate provides the same performance as EC2.
All code is here https://github.com/Aleksandr-Filichkin/ec2-vs-fargate
Test setup:
- ECS cluster with a public subnet
- ALB
- DynamoDB with on-demand provisioning
Will compare two ECS setups:
- EC2 m5.large (2 cores, 8 GB memory)
- Fargate (2 cores, 8GB memory)
TEST 1 (Fibonacci numbers)
The first test is to calculate the Fibonacci number.
Load scenario: send 50 parallel users from JMeter.
Result
We have exactly the same performance for EC2 and Fargate tasks for the Fibonacci numbers:
3000 request per minute and ~100% CPU utilization
TEST 2(DynamoDB post)
The first test was too synthetic. So the second test is to save JSON into DynamoDB. DynamoDB is configured for on-demand provisioning. It means DynamoDB will not be a bottleneck.
Code is here.
Load scenario: send 400 parallel users from JMeter.
Result
EC2 has 2787 requests per second and Fargate 2216. Two services have ~100% CPU utilization.
EC2 is 25% better than Fargate for this test case
TEST 3(DynamoDB post and 5 get calls)
Since we see that Fargate has some slowness for I/O operations I would like to run more I/O intensive tests. We will save a book and concurrently get it 5 times(count=5) from DynamoDb.
Code you can see here.
Load scenario: send 400 parallel users from JMeter.
Result
EC2 has 728 request per second and Fargate 454. Two services have ~100% CPU utilization.
EC2 is 40% better than Fargate for this test case
The bottleneck is CPU as you can see:
PS
I run test 3 for 4-cores Fargate and it 2 times better than 2-cores Fargate. Let’s check Dynamodb read metric for all 3 setups(EC2 2core, Fargate 2cores, Fargate 4cores)
Conclusion:
Fargate demonstrated good stable performance for the synthetic test, but in real life, you will see 25–40% performance degradation with I/O AWS services such as (DynamoDb, SQS, SNS, S3, etc). The problem is in CPU utilization, Fargate needs 2 times more CPU for I/O operations. From my point of view, it’s too slow and it’s better to support EC2. Otherwise, you need almost 2 times bigger setup for Fargate.
If you like it, please read my new post:
Fix Java cold start in AWS lambda with GraalVM [performance comparison]