race car

How to Make Lambda Faster: Memory Performance Benchmark

Configuring a Lambda function is considered 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:

  1. Faster function duration — In some cases it means less latency for your customers!
  2. Higher costsPricing 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.

Results

Without any further ado, let’s explore the chart:

AWS Lambda duration results

We can definitely observe that (memory) size matters! More memory dramatically reduces duration.

Duration improvement and price

Price vs performance

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.

Memory, price and duration

Conclusion

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 seeing results.