auremar - Fotolia

Strategies to accelerate AWS Lambda functions

In this chapter excerpt from Serverless Architectures on AWS, author Peter Sbarski shares five tricks developers can use to improve Lambda function performance.

IT teams that run a serverless architecture on AWS can build applications at scale to replace traditional silos and servers. AWS helps developers create serverless environments that meet compliance, security, maintenance and uptime needs.

AWS Lambda, the cloud provider's serverless computing service, creates a more efficient architecture. But the efficiency of that architecture depends on functions performing as expected. Developers should perform tests on AWS Lambda functions, including how long it takes a cold function to execute. On the initial test, the Lambda function is cold. On the second test, Lambda reuses the function -- now called a warm function -- and lowers the execution time.

The book, Serverless Architectures on AWS, by Peter Sbarski, dives into the steps IT can take to develop a serverless environment, write AWS Lambda functions and recognize different patterns in their architectures. This excerpt from Chapter 6, Lambda the orchestrator, gives the reader a look at how functions in Lambda run within a container and how container reuse can reduce AWS Lambda function duration times.

Cold and warm Lambda

Here's an experiment. Create any simple Hello World function in the AWS console and run it. You can easily do this by using the hello-world blueprint and then clicking the Test button in the console. Have a look at the duration in the summary in the bottom-left corner.

The time it takes to run a cold function is nearly 90 milliseconds.
The duration (89.28 milliseconds) is slow when the function is cold.

Then run the test again and look at the duration in the summary.

A warm function runs much quicker than a cold one.
The duration (0.48 milliseconds) when the function is warm.

If you compare the duration of both executions, you'll see that the time it takes to run a function for the first time is a lot longer than running it a second time. This is the result of the container reuse we described in the previous section. The first time the function is run (when it's cold), the container needs to be created and the environment needs to be initialized. A lengthy initialization time may be especially noticeable in complex functions that have multiple dependencies. Reusing a container and running a function again is almost always much quicker.

You should try to reduce cold starts (when a function hasn't been run for a long time and needs to fully initialize) to make the application appear more responsive. If you experience many cold starts, you can try a few steps to increase performance:

1. Schedule the function (using scheduled events) to run periodically to keep it warm (http://amzn.to/29AZsuX).

2. Move initialization and setup code out of the event handler. If the container is warm, this code won't run.

3. Increase the amount of memory allocated to the Lambda function. The CPU share is (proportionally) based on the amount of memory allocated to the function. AWS gives an example: "If you allocate 256 MB to your Lambda function, it will receive twice the CPU share than if you allocated 128 MB" (http://amzn.to/23aFKif). The more memory and CPU share the function has, the quicker it will initialize.

4. Reduce as much of your code as possible. Remove unnecessary modules and requires() import statements. Fewer modules to include and initialize will help startup performance.

5. Experiment with other languages. Java has the longest cold start. This may change in the future, but if you notice long cold starts using Java, try one of the other languages.

Manning Publications offers a 39% discount on Serverless Architectures on AWS for SearchAWS readers. Enter the code "serverlesstt" at checkout to receive the discounted price

Next Steps

Avoid these common Lambda errors

Get a visual representation of your Lambda deployment

Native tools for testing AWS application performance

Dig Deeper on AWS cloud development