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.
Cloudwatch lambda first log: 2025-01-02T19:27:23.208Z
Cloudwatch lambda last log: 2025-01-02T19:27:29.128Z
Cloudwatch says operation lasted 6 seconds.
However, on the client side I added a console.time and logs are:
Start time client: 2025-01-02T19:27:14.882Z
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
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)
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:
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
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.