Taking the Next Leap with AWS Lambda: Advanced Optimization Techniques

Andreas Kihlberg
AWS Specialists
Published in
6 min readMay 26, 2023
Taking the next leap: Advanced Optimization Techniques for AWS Lambda.

As cloud practitioners deeply immersed in AWS Lambda, we’ve all experienced the flexibility and scalability it provides. AWS Lambda’s event-driven, serverless computing platform, which runs your code in response to events and automatically manages underlying resources, has transformed how we build and operate our systems. However, as we continue to navigate through the endless potential of this service, it becomes imperative to understand the advanced optimization techniques that will help us take the next big leap.

Today, I’m excited to delve into the advanced AWS Lambda optimization techniques to extract maximum performance and efficiency from your Lambda functions.

Optimizing Function Memory and Timeout Settings

The default settings for memory (128 MB) and timeout (3 seconds) may not always serve your function requirements best. Optimal memory settings can reduce the execution time of your functions, saving costs in the long run.

For instance, a compute-intensive function might benefit from higher memory allocation, leading to lower execution times. A trade-off exists between memory size and cost, and identifying the right balance is key.

Screenshot over memory settings for a Lambda function
Showing different settings for a Lambda function in AWS Console

The default memory allocation for a Lambda function is 128 MB which is fine for many situations, but in this image, we are running a headless chrome that will load different types of websites for testing and image generation. This requires more memory and we decided to go with 1024 MB. The timeout settings chosen accommodate pages that might load slowly.

Note that there is no possibility to change the CPU alone. It is related to the memory, and more memory gives more CPU.

The timeout setting is equally important, as functions that run longer than necessary can accrue unnecessary costs. A systematic approach toward understanding your function’s behavior and adjusting these settings can lead to significant cost and performance optimizations.

To help you optimize your memory and CPU settings, you can use a tool like AWS Lambda Power Tuning to try out different settings.

Understanding and Minimizing Cold Starts

Cold starts occur when AWS has to set up a new execution context for your function, resulting in a longer startup time. They happen when your function hasn’t been invoked for some time or following a new deployment.

While this overhead is minimal for most applications, it can become significant for latency-sensitive applications. However, you can mitigate this by:

  • Keeping your functions warm by setting up a CloudWatch Event or using EventBridge that triggers your function every few minutes.
  • If you are using Java as a programming language in your Lambda function, you can enable Lambda SnapStart to improve the startup performance of your function.
  • Minimizing your deployment package size: Smaller deployment packages boot faster because they require less time to download and unpack.
  • Using provisioned concurrency: This feature lets you keep a specified number of execution environments initialized and ready to respond to your function. This will affect the cost.

Concurrent Executions

Each AWS account has a limit on the number of concurrent executions across all functions within a region. The default limit is 1000. Hence, if you have functions that experience high spikes in traffic, it’s critical to ensure you’re not hitting this limit to prevent throttling. If required, you can request an increase in this limit.

It’s possible to reserve concurrent executions for a single function, which might be a good choice if you have critical functions that need to run without being throttled.

Use DLQ or Lambda destinations on asynchronous invocations

There are two ways to invoke a lambda function, synchronous or asynchronous. When invoking a function synchronously, it will return a response immediately. An example where synchronous invocation is used is API Gateway, where the client waits until the function is done.

When invoking asynchronously, the client still gets an immediate response that the function received the event, but it will keep working in the background. So what happens if there is an error in the function? The client is long gone, and you can’t notify it. That’s where Dead letter queues (DLQ) or Lambda destinations come in handy.

If your function fails for some reason during an asynchronous invocation, the error is passed on to a queue or a Lambda destination so you have a chance to process the error and take action.

A Lambda function in the AWS Console with an S3 trigger and EventBridge as the destination.

The image shows a Lambda function in the AWS Console, where s3 triggers the function, and EventBridge is the destination if the function fails. Via EventBridge, you can then handle the error, for instance, send an email or take any other action.

The destination can be set up in the console or preferably with Infrastructure as Code (IaC).

By using destinations or DLQ, you don’t have to handle any errors in the function itself, and if you have multiple functions, you don’t have to handle the error in every function. It results in a function with less code, and you can easily manage all errors in the same place.

It’s preferred to use Lambda Destinations over DLQ. It will give you more information about what went wrong and support more AWS Services, such as SNS, SQS, Lambda, and EventBridge.

Fine-tuning with X-Ray

AWS X-Ray is an invaluable tool for diagnosing issues in your Lambda functions. It helps you understand how your application and its underlying services perform and where bottlenecks occur. In addition, you can use it to trace requests from start to end and get a map of services in your application.

A map over a serverless application containing API Gateway, Lambda, and DynamoDB.

First, you get a nice visual overview of the application on how the services connect. Second, you get access to a timeline where you can pinpoint how much time every step takes. It makes it easy to detect and locate bottlenecks in a serverless application.

X-Ray also allows you to add your custom metadata and subsegments to get even more details.

Utilizing Layers and Extensions

Layers let you manage your Lambda function code better by separating your function code from your resources. You can share code, libraries, and other components with layers across multiple functions.

AWS Lambda Extensions can be used to integrate Lambda functions with your preferred tooling for monitoring, observability, security, and governance. In addition, they can run additional code during the lifecycle of a Lambda function invocation to help you accomplish these tasks.
In the image below, we have used a layer, the Cloudwatch Insight extension. This extension gives you more insights into the performance of your lambda function.

The extension for Cloudwatch Lambda Insight is activated. This allows for a more detailed view of the functions' performance.

This feature gathers, consolidates, and presents system-level metrics, encompassing CPU time, memory usage, disk activity, and network performance. Additionally, it captures, consolidates, and provides a concise overview of diagnostic details like cold starts and Lambda worker shutdowns. These capabilities are designed to assist in identifying and isolating any issues with your Lambda functions, enabling you to address them promptly and effectively.

In conclusion, AWS Lambda is not a “set it and forget it” service. Instead, it requires constant monitoring, tweaking, and tuning to perform at its best. By employing these advanced optimization techniques, you’ll be able to harness the full power of AWS Lambda.

So, gear up to take the next leap with AWS Lambda. The sky is truly the limit!

Until next time, happy optimizing!

--

--

Andreas Kihlberg
AWS Specialists

Web developer with passion for great architecture, smart solutions and new technologies