How to make Lambda faster: memory performance benchmark
Configuring a Lambda function is considered as a very complex task. Among all parameters, you will have to pick the memory size for your function, which is very confusing.
Developers never test their code’s memory consumption, definitely not in every use case. It then makes us select a random memory size for our function.
Not everyone knows, but the memory selection affects proportionally on the allocated CPU. Currently, AWS Lambda supports between 128MB up to 3008MB to choose from.
More CPU allocated basically means:
- Faster function duration — In some cases it means less latency for your customers!
- Higher costs — Pricing increases proportionally.
But then, the following question arises:
If a function is running faster, will it be cheaper?
So the answer is not that simple. We will try to cover that with the following benchmark and results.
Benchmarking: Fibonacci recursion in Python
To visualize performance results in charts, we built a simple Python code that calculates Fibonacci sequence in recursion and loaded it to a Lambda function.
With an automated benchmark script, we could easily test all possibilities of memory size. The benchmark script already pre-warmed the function so we won’t have any cold start delays.
Without any further ado, let’s explore the chart:
We can definitely observe that (memory) size matters! More memory dramatically reduces duration.
As we can see, there’s a high correlation between shorter durations and price differences for different memory sizes. Besides, we see that at some point (2048MB => 3008MB) the performance does not increase at the expected rate, while the price does.
So it is definitely an important task to pick the right amount of memory for our functions. The tradeoff is potentially higher costs vs. shorter duration times, which leads to lower latency if the function is facing customers and users.
Our recommendation is to run a few manual tests on functions to have a clue about their durations and then decide according to the results.
You can find the Fibonacci function with the benchmark script at Epsagon’s open source repository: lambda-memory-performance-benchmark. Feel free to contribute!
*UPDATE* : you can now use the benchmark tool to test your own functions! looking forward to see results.
Interested in learning more about dynamic monitoring for serverless architectures? Sign up for our private beta at https://epsagon.com.