Lesson 25 / 32

Reducing Cold Start Impact

Shrink cold start time with smaller bundles and smarter init code, and know when to pay for pre-warmed capacity.

By · 15 years in web development

Make cold starts smaller and rarer

Cold start time is mostly your init work plus the runtime's own startup. The levers, roughly in order of effort: ship less code (bundle and tree-shake with a tool such as esbuild, import only the SDK clients you use, avoid huge dependencies); do expensive setup once at module scope, but lazily if only some requests need it; choose the runtime carefully, because Node.js and Python usually start faster than a JVM unless you use its startup optimisations; and give more memory, which on Lambda also gives proportionally more CPU to the init phase. To hide cold starts entirely you pay for capacity: provisioned concurrency keeps a set number of environments initialised and is billed while configured, and SnapStart (first for Java, now also some other runtimes) restores a snapshot of an already-initialised environment. On isolate-based edge runtimes cold starts are already small, so focus there on bundle size and CPU time.

Bundling a handler with esbuild

One minified file with only the code you actually import. The AWS SDK v3 is excluded because Lambda's Node.js runtimes include it; bundle it yourself if you need a pinned SDK version.

npx esbuild src/handler.mjs --bundle --platform=node --target=node22 \
  --format=esm --minify --outfile=dist/index.mjs \
  '--external:@aws-sdk/*'

Three levers for production readiness

Performance and safety work in this section follows three steps: cut cold start time, control concurrency, and lock down permissions.

Three connected stages from left to right: a shorter cold start, a concurrency limit, and a least-privilege permission boundary.
Figure 9.1: shrink cold starts -> control concurrency -> enforce least privilege

Measure before optimising

On Lambda, the REPORT log line of a cold invocation includes an Init Duration field. Chart it alongside p99 latency. If cold starts are 1% of requests and users never notice, provisioned concurrency may be money spent on nothing.

Quick check: What does provisioned concurrency on Lambda do?

  • Removes the per-region concurrency quota
  • Makes every function run on dedicated hardware for free
  • Bundles and minifies your code automatically
  • Keeps a configured number of environments initialised and ready, billed while it is enabled
Answer

Keeps a configured number of environments initialised and ready, billed while it is enabled — It pre-initialises environments so requests up to that level avoid cold starts, and you pay for that capacity whether or not it is used.