Tips to improve AWS Lambda performance

wakeupmh

Marcos Henrique

Posted on January 23, 2023

Tips to improve AWS Lambda performance

If you're a developer, you've probably heard of AWS Lambda. It's a service that allows you to run code without having to worry about any of the infrastructure that usually comes with it.

But what does that mean for you, as a developer? And how can you use it to your advantage? Here are some of the benefits and best practices for using AWS Lambda:

  • You don't have to worry about setting up servers or managing them after they're set up--it's all taken care of by Amazon.

  • You can focus on writing your code instead of worrying about whether or not it'll run on the server you've chosen.

  • You save money by only paying for what you use (and not paying for unused resources).

How to create performance-optimized lambdas?

  • Use the right runtime

There are many different runtimes for AWS Lambda, but for performance-optimization purposes, you'll want to use Node 8.10.x or later. The Node 8.10 runtime includes support for async/await, which makes your code more efficient by allowing you to avoid context switching between threads and processes.

  • Use memory efficiently

AWS Lambda limits each function invocation to 1GB of memory, and you'll want to keep that in mind when writing your code. You can use heapdumpster (an open-source tool) to analyze the memory usage of your application and find out if there are any leaks or other issues that could cause problems with memory usage at scale.

  • Use compression wisely

Compressing data before sending it over the wire can help reduce bandwidth costs, but it also takes CPU time on both ends--so don't compress things unless they're going over HTTP! If your data is already compressed (like JSON), then there's no point in doing it again--just send the compressed version instead!

  • Don't use the default timeout

The default timeout setting is 300 seconds, which means that after 300 seconds have passed without any activity from your lambda function, it will be terminated by AWS Lambda. Instead, set your own timeout value (e.g., 30 seconds) so that if there are any unexpected delays in processing or communication between your lambda function and other services, you can ensure that these delays don't cause your lambda to terminate prematurely.

  • Use Arm-based AWS Graviton2 processors on your lambdas

AWS Graviton processors are custom silicon from Amazon’s Annapurna Labs. Based on the Arm processor architecture, they are optimized for performance and cost, which allows customers to get up to 34% better price performance.

This AWS Compute Blog post discusses some of the differences between the x86 and Arm architectures, as well as methods for developing Lambda functions on Graviton2, including performance benchmarking.

Many serverless workloads can benefit from Graviton2, especially when they are not using a library that requires an x86 architecture to run.

  • Reuse your TCP Connections The default Node.js HTTP/HTTPS agent creates a new TCP connection for every new request. Usually this is not a cause for concern, but for short-lived operations like DynamoDB queries it's possible that establishing a TCP connection can take longer than the operation you want to execute.

Make sure that AWS_NODEJS_CONNECTION_REUSE_ENABLED environment variable is set to true.
Once this environment variable is set, your AWS Lambda functions will no longer establish a new TCP connection every single time and gain something like ~100ms performance improvements

💖 💪 🙅 🚩
wakeupmh
Marcos Henrique

Posted on January 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related