Skip to content
WritingArticle
  • #serverless
  • #aws
  • #lambda
  • #performance

AWS Lambda Cold Starts Explained: What Causes Them and 8 Ways to Reduce Them

Pawan Joshi

6 min read

Share

Every serverless developer eventually notices it: the first request to a function takes noticeably longer than the next hundred. That's a cold start.

Cold starts are real, but they're often smaller than people fear, and most of the delay is under your control. Here's what happens, how to measure it, and how to make it smaller.

What happens on a cold start

Lambda runs your function inside an execution environment: a small, isolated micro-VM with your runtime and code. Each environment handles one request at a time and is reused for later requests.

A cold start happens when Lambda has no idle environment ready for a request, so it must create one. That means:

  1. Create the environment and download your code package or container image.
  2. Start the runtime, for example Node.js or Python.
  3. Run your init code: everything outside the handler, such as imports, SDK clients and config loading.
  4. Run the handler for the request.

On a warm start, Lambda reuses an existing environment and jumps straight to step 4.

Cold starts happen on the first request after a deploy, when traffic rises and Lambda adds environments, and after an environment has been idle long enough to be recycled.

Measure before you optimize

Don't guess. Every invocation writes a REPORT line to CloudWatch Logs. On a cold start it includes an extra field:

REPORT RequestId: 3f2c...  Duration: 42.10 ms  Billed Duration: 43 ms
Memory Size: 512 MB  Max Memory Used: 88 MB  Init Duration: 312.45 ms

Init Duration is the cold start cost of steps 1 to 3. If it's missing, that request was warm.

Find your worst cold starts with CloudWatch Logs Insights:

filter @type = "REPORT" and ispresent(@initDuration)
| stats count() as coldStarts, avg(@initDuration) as avgInitMs, max(@initDuration) as maxInitMs by bin(1h)

Also check how often cold starts happen. For many APIs, they're a small fraction of requests, which changes how much effort is worth spending.

Keep in mind that the init phase counts toward cost too, so a lighter init saves money as well as time.

What makes cold starts slow

CauseWhy it matters
Large deployment packageMore code to download and load
Heavy imports at startupLoading a whole SDK or framework takes time
Slow work during initNetwork calls, secrets lookups or database connections before the first request
RuntimeInterpreted runtimes like Node.js and Python usually start faster than the JVM without SnapStart
Low memory settingLambda gives CPU in proportion to memory, so init runs on less CPU
VPC attachmentMostly solved by AWS's improved VPC networking, but extra network setup still has a small cost

8 ways to reduce cold starts

1. Ship less code

Bundle and tree-shake with esbuild so only the code you use is deployed. AWS SAM can do this for Node.js:

NotesFunction:
  Type: AWS::Serverless::Function
  Metadata:
    BuildMethod: esbuild
    BuildProperties:
      Minify: true
      Target: es2022
      EntryPoints: [handler.mjs]

Remove unused dependencies. A function that imports one helper from a huge library pays to load the whole library.

2. Import only what you need

The AWS SDK v3 is modular. Import the one client you need, not everything:

// Good: loads only the DynamoDB client
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";

3. Do expensive setup once, outside the handler

Code outside the handler runs once per environment and is reused by every warm request. Put SDK clients and config there.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const db = new DynamoDBClient({});   // created once, reused
 
export const handler = async (event) => {
  // use db here
};

But keep init lean. Don't make slow network calls at the top level if only some requests need them. Load those lazily inside the handler and cache the result.

4. Give the function enough memory

More memory also means more CPU, so init and handler code run faster. Sometimes doubling memory makes the function fast enough that it costs about the same or less. Test with AWS Lambda Power Tuning to find the best setting instead of guessing.

5. Use arm64 (Graviton)

arm64 functions are priced lower per millisecond than x86, and many workloads run as fast or faster. Switching is often one line in your template: Architectures: [arm64]. Test any native dependencies first.

6. Pick the runtime for the job

Node.js and Python start quickly and suit most APIs. Java and .NET can be very fast once warm, but start slower without help, which is where SnapStart comes in.

7. Use Lambda SnapStart where it's supported

SnapStart runs your init code once when you publish a version, takes a snapshot of the initialized environment, and resumes new environments from that snapshot. It's supported for Java, Python and .NET runtimes and can cut cold starts dramatically, especially for Java.

Two rules when using SnapStart:

  • Anything that must be unique, such as random seeds or temporary credentials, must be generated after restore, not during init.
  • Re-establish network connections after restore. SnapStart provides runtime hooks for this.

8. Use provisioned concurrency for latency-critical paths

Provisioned concurrency keeps a set number of environments initialized and ready, so those requests never see a cold start. You pay for it while it's configured, so use it where latency matters most, like a checkout or login API, and scale it on a schedule with Application Auto Scaling.

What not to do

  • Don't "ping" functions to keep them warm. Scheduled pings keep only one environment warm, don't help when traffic scales up, and add cost and noise. Provisioned concurrency or SnapStart solve the problem properly.
  • Don't optimize blindly. If cold starts are 2% of requests and add 200 ms, a smaller package may be all you need.
  • Don't move everything into init. Init that's too heavy just makes every cold start slower.

Design so cold starts matter less

The best fix is often architectural:

  • Go asynchronous where users don't wait. Put work behind SQS or EventBridge. A cold start on a background job is invisible. See Event-Driven Serverless with EventBridge, SQS and Lambda.
  • Cache at the edge. Serve read-heavy responses from CloudFront so many requests never reach Lambda.
  • Split big functions. A small function with one job has fewer imports and a faster init than a giant "do everything" function.

Checklist

  1. Measure Init Duration and how often cold starts happen.
  2. Bundle with esbuild and remove unused dependencies.
  3. Import only the SDK clients you use.
  4. Create clients outside the handler, but keep init lean.
  5. Tune memory with Lambda Power Tuning.
  6. Try arm64.
  7. Use SnapStart for Java, Python or .NET where it fits.
  8. Add provisioned concurrency only for latency-critical paths.

Want the hands-on version first? Build and measure your own function in Build a Serverless REST API with AWS Lambda, API Gateway and DynamoDB.