CodePark/ questions / Lambda & serverless
START PRACTICINGPRACTICE
Backend question · Cloud & AWS › Lambda & serverless

What is a Lambda cold start, and how do you reduce its impact for a Node.js function?

A cold start happens when Lambda has no free, ready environment for a request. Before your handler runs, Lambda must:

  • create the environment
  • download your code
  • start the runtime (Node.js)
  • run your init code, which is the code outside the handler

A warm request reuses an environment that already exists, so it skips all of that.

To make cold starts shorter:

  • Keep the deployment bundle small. Bundle and tree-shake (remove unused code) with esbuild.
  • Remove unused dependencies. Import only the AWS SDK v3 clients you need, not the whole SDK.
  • Keep top-level init code cheap. Lazy-load modules you rarely use, which means load them only when you need them.
  • Avoid heavy frameworks at startup. Starting a full NestJS app on every cold start is slow, unless you cache the app instance and reuse it.
  • Give the function more memory. More memory also gives more CPU, so init runs faster.

For endpoints where speed matters, use provisioned concurrency. It keeps a set number of environments started and ready ahead of time. It costs extra.

  • A cold start means: create the environment, load the code, start the runtime, then run init code.
  • Make bundles smaller with esbuild and with only the SDK v3 clients you need.
  • Keep init cheap, lazy-load rarely used code, and cache the NestJS app instance.
  • More memory gives more CPU, which makes init faster.
  • Provisioned concurrency keeps environments started and ready ahead of time.