Reduce the Latency of Your AWS Lambda Functions

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

You may notice that sometimes when you make a query or mutation after not using your app for a while, it will be extra slow. What gives?

There are a couple of things happening. First, a lambda function goes through a "cold-start" after being run for the first time in a while. AWS is downloading the code and spinning up an environment.

Second, if you are using the default settings, your function doesn't have much memory to work with. You can specify the amount of memory your function has available in its configuration.

Tomasz Łakomy: [0:00] Let's talk about performance. I haven't called this API in a while. If I hit Play, you will notice that the request is taking some time to return. If I hit Play again, now it's much faster. Let's investigate what exactly is happening here.

[0:15] To investigate the latency of our function, let us go back to AWS Console. I'm going to navigate to the CloudFormation console, and next, I'm going to navigate to my stack. Next up, I'm going to click on Resources and search for a Lambda. We can see two Lambda functions here. I'm going to click on the listBooks handler.

[0:33] Here, in the Lambda console, we can see the source code of our function, transpiled from TypeScript to JavaScript. What I care about are the logs. I'm going to click on Monitor, and next, click on View Logs in CloudWatch. Here, we can see the logs for our listBooks handler.

[0:49] First, let me scroll down to the newest log stream. Those are the logs of our Lambda function. At the end of each invocation, there's a report. Here, for instance, we can see that this particular invocation took 709 milliseconds.

[1:03] Because this was a cold start, the Init Duration was close to 400 milliseconds. This is a bit slow. It's getting a bit better once the function is warm. For instance, in this particular invocation, it took 83 milliseconds. There was no cold start.

[1:18] There's a way to improve the performance of a Lambda function. Note that currently, the memory size assigned to this function is 128 megabytes, which is frankly not a lot. In fact, this Lambda function is using 78 megabytes of its memory.

[1:32] Let's increase the memory available for this function. To do that, go back to our stack. Inside of listBooksLambda, I'm going to also specify the memory size, which is a number. I'm going to provide it with a gigabyte of memory, 1024 megabytes. If I save that, I can go ahead and deploy it.

[1:52] After it's done, go back to the Lambda console. Here, I can click on Configuration in order to verify that my function now has a whole gigabyte of memory available to it.

[2:02] Let's test if there's any improvement. Go back to our GraphQL Client. I'm going to invoke this function a bunch of times. Go back to Monitor. Click on View Logs in CloudWatch once again. Click on the newest log stream. I'm going to expand all of that.

[2:17] Here, we can see that even though we did hit a cold start, the duration of the function was 85 milliseconds, which is much faster. Here, we can see that the memory available for this function was a whole gigabyte.

[2:31] Again, the max memory used by this function was roughly 77, 78 megabytes. It gets even better once there's no cold start. Those last few executions were around 23, 28, and 22 milliseconds, which is rather fast.

michaelbruns1
michaelbruns1
~ 2 years ago

Amazing!

Markdown supported.
Become a member to join the discussionEnroll Today