Significant Slowdown in AWS Lambda Cold Starts with Node.js 14.x and Express.js Application
I'm prototyping a solution and I'm a bit lost with I'm relatively new to this, so bear with me... I'm experiencing considerable latency when my AWS Lambda function cold starts, specifically with my Node.js 14.x application that uses Express.js. The initial invocation takes around 5-6 seconds, which is unacceptable for my use case. I've enabled the Lambda provisioned concurrency to mitigate this issue, but I'm still seeing high latency during the first request after a period of inactivity. To give you some context, my function is set up as follows: ```javascript const express = require('express'); const serverless = require('serverless-http'); const app = express(); app.get('/', (req, res) => { res.send('Hello World'); }); module.exports.handler = serverless(app); ``` I've tried optimizing the Lambda function by reducing the package size, using only necessary dependencies, and even experimenting with the `NODE_OPTIONS` environment variable to minimize the memory footprint. However, the cold start time remains largely unchanged. I also considered that my function might be too heavy because it imports several libraries, but I’m already using `serverless-http` which is intended to help. Additionally, I’ve made sure that the timeout setting on the Lambda function is set to 30 seconds, which should be sufficient for the initial request. I’ve checked the AWS Lambda metrics, and I can see a pattern where the duration spikes after a period of inactivity. However, I am unsure if the latency is primarily due to the function size, the Express.js framework, or the underlying configuration. Is there a way to further optimize this cold start issue? Are there specific best practices for improving Lambda cold start performance with Node.js and Express? Any insights would be greatly appreciated! My team is using Javascript for this microservice. What's the best practice here? For reference, this is a production desktop app. Is there a simpler solution I'm overlooking? I'm working with Javascript in a Docker container on Windows 11.