How to reduce cold-start? #lambda

Hello!

I would like to ask help in ways to reduce lambdas cold-start, if possible.

I have an API endpoint that calls for a lambda on NodeJS runtime. According to Cloudwatch logs, the request operation takes 6 seconds. However, I want to attach logs because total execution time is actually 14 seconds… this is like 8 seconds of latency.

  1. Cloudwatch lambda first log: 2025-01-02T19:27:23.208Z

  2. Cloudwatch lambda last log: 2025-01-02T19:27:29.128Z

  3. Cloudwatch says operation lasted 6 seconds.

However, on the client side I added a console.time and logs are:

  1. Start time client: 2025-01-02T19:27:14.882Z

  2. End time client: 2025-01-02T19:27:28.839Z

Is there a way to reduce this cold start? My app is a chat so I need faster response times

Thanks a lot and happy new year!

  1. Increase memory (this increases CPU proportionally)
  2. Use a newer NodeJS runtime
  3. Do less work in the initialization phase

Those are your options.

8 seconds seems crazy long for node. Ensure that you’re not packaging anything that you don’t need to be, you’re only initializing things that you’re actually using, and that your memory size is set appropriately (aka not 128 MB)

  1. I was using 256 mb. I’ve just upgraded to 2048 and i’m amplify pushing. Will let u know!
  2. Im using node 18, do you suggest upgrading?
  3. can you tell me more about this?

Thanks a lot!

The issue gotta be in your code. The cold-start for Lambda with NodeJS runtime shouldn’t be more than ~150ms max. What does your Lambda function use? One tip is trying to increase the amount of memory for the Lambda function. This might actually be a lot cheaper in the end if your Lambda uses a lot of CPU or networking.

I’ve upgraded to 2048mb and seemed like it helped! Same payload lasted 8.5 seconds total, instead of 14 seconds. It’s still a lot of time, though. The cold-start right now went down to 4seconds.

I’ve upgraded to 2048mb and seemed like it helped! Same payload lasted 8.5 seconds total, instead of 14 seconds. It’s still a lot of time, though. The cold-start right now went down to 4seconds.

Package is not that big, at least the package.json from the root lambda:

“dependencies”: {
“aws-serverless-express”: “^3.3.5”,
“body-parser”: “^1.17.1”,
“cheerio”: “^1.0.0”,
“express”: “^4.15.2”
},
“devDependencies”: {
“@types/aws-lambda”: “^8.10.92”
}

I’ve just looked and cheerio is like 1.25mb in npm. I’ll try to look for an alterantive.
I have some lambda layers running on this lambda, with their own packages too.

For NodeJS, this is a terrible cold-start number. How large is your function (you can look at the ‘Code size’ column in the function list)? Do you have any Layers attached? If so, how large are they?

It just feels like you’re loading a ton of data into the Lambda, and that’s taking a lot of time.

I’ve upgraded to 2048mb and seemed like it helped! Same payload lasted 8.5 seconds total, instead of 14 seconds. It’s still a lot of time, though. The cold-start right now went down to 4seconds.

My code doens’t seem like it could be adding to the huge cold start. Im using the amplify express template integrated with api gateway. I’m only importing these in my app.js

const express = require(“express”);
const bodyParser = require(“body-parser”);
const awsServerlessExpressMiddleware = require(“aws-serverless-express/middleware”);
const cheerio = require(“cheerio”);

Are you using a ZIP file or Docker image for the Lambda function? Also how big is the ZIP file/Docker image?

That’s something interesting to look at! I’ve just checked and the lambda info is:

||
||
|babbleoneRestApi-develop|-|Zip|Node.js 18.x|x86_64|2.9 MB|2048|18 minutes ago|

That’s something interesting to look at! I’ve just checked and the lambda info is:

||
||
|babbleoneRestApi-develop|-|Zip|Node.js 18.x|x86_64|2.9 MB|2048|18 minutes ago|

That’s something interesting to look at! I’ve just checked and the lambda info is:

||
||
|babbleoneRestApi-develop|-|Zip|Node.js 18.x|x86_64|2.9 MB|2048|18 minutes ago|

Zip, 3mb

Like others asked, you should check the size of the layers attached. That might still be an issue. Also are you using any Lambda extensions?

Do less work in the initialization phase

Balanced with not doing work per invoke that can be initialized once?

I have around 5 lambda layers, ranging from 200 kb to 4mb in size. I checked and the largest ones have the node_modules included in the zip. I guess it should have been ignored when pushing.

You’re probably doing a lot of work outside the handler (as soon as the file is imported). If a lot of that can be reused fairly perpetually, SnapStart could help a lot. It’s worth trying anyway. May require some code changes depending on the assumptions you’ve made.

Unfortunately, SnapStart is not available for node environment :frowning: