Compute Runtime
We at Statista decided to use the compute pricing model in AWS. This means all new Application must either run on Fargate or Lambda. This is a decision to reduce the complexity of the infrastructure and to reduce the costs of running applications by beeing able to buy Compute Savings Plans.
We dont want to buy Servers anymore. We want to buy CPU-Cycles! This is much more flexible!
The question is now: How to choose between Fargate and Lambda?
Fargate
Fargate is a serverless compute engine for docker containers. They can scale up and down and can be provisioned with CPU and memory resources. Fargate is a good choice for applications that require a lot of CPU and memory resources, or for applications that need to run for a long time.
you pay per second for the resources you use. The minimum is 0.25 VCPU and 512MB of memory. The maximum is 16 VCPUs and 120GB of memory.
see here for a full list of the available task sizes.
if possible use ARM based instances, as they are cheaper and faster than x86 based instances.
Fargate requires a lot of additional Network Setup (LB, Security Groups, VPC, etc.) and is a bit more complex to setup than Lambda.
Lambda
Lambda is a serverless compute engine for running code in response to events. It is a good choice for applications that require low latency and high throughput, or for applications that need to run for a short time or has frequent access. Lambda is also a good choice for applications that require a lot of scaling, as it can scale up and down automatically based on the number of requests (1000 parallel Lambdas are a hard limit on AWS). So if you have more than 1000 concurrent Requests that take more than 1s, Lambda isnt the right choice.
you pay per request and per duration of the request. The duration is calculated from the time your code begins executing until it returns or otherwise terminates, rounded up to the nearest 1ms. The minimum is 128MB of memory and the maximum is 10GB of memory. The maximum execution time is 15 minutes. CPU scales linear with Memory.
see here for a full list of the available memory sizes.
Lambda is a way easier setup than Fargate, as you dont have to worry about the underlying infrastructure.
Pricing Example
those examples assume we have no Compute Savings Plans.
Lambda Scenario
512 MB RAM
each request takes 1 second
| Requests per Month | Cost |
|---|---|
| 50,000 | 0$ (free tier) |
| 100,000 | 0$ (free tier) |
| 1,000,000 | 1.7$ |
| 10,000,000 | 79$ |
| 100,000,000 | 850$ |
see here for a simple cost calculator.
Fargate Scenario
running 24/7 the entire Month
| VCPU | RAM | Cost |
|---|---|---|
| 0.25 | 512MB | 8$ |
| 1 | 2GB | 32$ |
| 2 | 4GB | 65$ |
see here for a simple cost calculator.
Conclusion
assuming a constant load of requests over the month.
100 Million Requests per Month are approx. 38 concurrent Requests per second.
While the tiniest Fargate Container maybe is able to serve this amount of
requests, there maybe latency problems, but even with the biggest Fargate Setup
of 16 VCPUs you will be cheaper
(523$) than the respective Lambda calculation (850$).
10 Million Requests per Month are approx. 0.3 concurrent Requests per second.
This can be handled easily by Fargate and is way cheaper than Lambda.
1 Million Requests per Month are approx. 0.03 concurrent Requests per second.
This can be handled easily by Fargate, but Lambda is cheaper (due to the free
tier).
Everything below 1 Million Requests per Month is a good candidate for Lambda because of the Free-Tier. For more than 5 Million Requests per Month, Fargate is the better choice. If your requests are way faster than 1s, Lambda can be the cheaper choice even for multi-million requests.
But for a good and safe Fargate Setup you would need at least 2 tasks, so the prices double for safety reason. Also Autoscaling (Traffic Spikes) can spike costs on Fargate.
This twists the calculation a bit, but in the end Fargate is the better choice for more than a few million requests per Month.
there is no golden rule to take either Fargate or Lambda.
Decision Points
- number of requests per month
- average request duration
- traffic flow (constant or spikes)
- concurrent requests
Examples
| Service | Scaling | Compute | Requests/Month |
|---|---|---|---|
| Monolith | 2x 8vCPU & 16GB RAM per region | Fargate | 270M |
| UAO | ? | Lambda | 750k |
| Global Components | ? | Lambda | 56M |
| Research-AI | ? | Lambda | 2M |
| SSO | 2x 1vCPU & 2GB RAM (only eu-central-1) | Fargate | 164M |