Skip to content

Understanding Lambda Performance and Cold Starts

What a cold start in AWS Lambda really is, which levers you control – from bundle size through init code and memory to Provisioned Concurrency – and how to measure all of it properly.

≈ 12 min readSoftwareAWSCloud

An endpoint responds in 40 milliseconds, on the next call in 900 – nothing changed in the code, and monitoring reports no error. The difference lies solely in whether AWS Lambda could reuse an existing execution environment or had to create a new one. And every millisecond of work that happens outside the handler is paid by the first caller of that new environment – fully and visibly.

This outlier is called a cold start, and its bad reputation is undeserved. It is neither a bug nor a sign of a poorly built function, but a direct consequence of Lambda’s execution model. Once you see through this model, the vague “sometimes slow” breaks down into a handful of concrete adjustment knobs – and a few stubborn misconceptions sort themselves out along the way.

What really happens during a cold start#

Lambda doesn’t run your code in a permanently running process but in a so-called execution environment. This is an isolated, short-lived runtime that Lambda creates on demand, keeps alive for a while and eventually throws away. The lifecycle of this environment consists of three phases: Init, Invoke and Shutdown.

A cold start is nothing other than the Init phase of a freshly created environment. Quite a lot happens before your actual logic even starts:

  • Lambda downloads your code – from internal storage or, for container images, from the registry.
  • The runtime is initialized, for example by starting the Node.js process.
  • Your function init code runs: everything you wrote at module level outside the handler – imports, creating SDK clients, reading configuration.

Only then does the Invoke phase follow, in which your handler is called with the concrete event. An important detail for practice: the Init phase has a hard timeout of 10 seconds. If you do expensive work at module level, you can actually hit this limit – more on that later.

After the invoke, Lambda doesn’t throw the environment away immediately. It is frozen and thawed again for another call within a short time. Then we speak of a warm invoke: the entire Init phase is skipped because the runtime and init code have already run. Only your handler runs. That is why the same endpoint sometimes takes 40 and sometimes 900 milliseconds – the difference is simply whether a warm environment was ready or a new one had to be created.

flowchart TD
    Req[Incoming invoke] --> Q{Warm environment<br/>available?}
    Q -->|No: cold start| C1[Download code]
    C1 --> C2[Initialize runtime]
    C2 --> C3[Run init code outside<br/>the handler]
    C3 --> H[Run handler]
    Q -->|Yes: warm invoke| H
    H --> F[Freeze environment<br/>for later invokes]
    F -.reuse.-> Q

The diagram makes the central point visible: the expensive part – download, runtime init, function init – only happens on the cold path. The “Init Duration” you’ll find in the logs in a moment is exactly the sum of these three steps. On every warm invoke, Lambda jumps straight to the handler.

The first lever: keep the bundle small#

The most obvious contribution to the cold start is the time it takes to load your code and parse the modules. The bigger the bundle and the more branched the import graph, the longer that takes. That sounds trivial, but I regularly see deployment packages of several megabytes in which a complete SDK, three date libraries and unused parts of a framework travel along.

The pragmatic answer is bundling with tree shaking. A bundler like esbuild throws out everything that isn’t actually reached and packs the rest into a compact file. A typical call looks like this:

esbuild src/handler.ts \
  --bundle \
  --minify \
  --platform=node \
  --target=node18 \
  --external:@aws-sdk/* \
  --outfile=dist/handler.js

Two things here are deliberate choices. The target is node18 – Lambda’s current LTS runtime at this point. And AWS SDK v3 is kept out of the bundle via --external:@aws-sdk/*, because it is preinstalled in the Node18 runtime anyway. Packing it in would only bloat the package unnecessarily without any benefit.

If you work with AWS SAM or the CDK, you often don’t have to take care of the call yourself: the NodejsFunction construct uses esbuild internally and bundles automatically. The point stays the same – less code and fewer dependencies mean a shorter download and faster module loading, and therefore a shorter cold start.

The second lever: init code in the right place#

Now comes the part that makes the biggest difference in practice and at the same time is most often done wrong: where do you create your clients and connections?

Everything you write at module level outside the handler runs exactly once per execution environment – in the Init phase. On every following warm invoke, the result is reused. That is exactly where everything belongs that is reusable and stable across calls: SDK clients, database pools, parsed configuration.

// module scope — runs once per execution environment (Init phase)
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { GetItemCommand } from "@aws-sdk/client-dynamodb";

// created once, reused across every warm invoke
const client = new DynamoDBClient({});

export const handler = async (event: { id: string }) => {
  const result = await client.send(
    new GetItemCommand({
      TableName: "orders",
      Key: { id: { S: event.id } },
    })
  );

  return { statusCode: 200, body: JSON.stringify(result.Item) };
};

The DynamoDBClient is created once here and then stays in the environment. Every warm invoke accesses the same instance – including the HTTP connections already established. This is where an often overlooked advantage of AWS SDK v3 lies: HTTP keep-alive is enabled by default. So the TCP and TLS handshakes only happen on the first call and are reused afterwards.

A short but practically relevant note on migration: with the older AWS SDK v2, keep-alive was not on automatically. There you have to set the environment variable AWS_NODEJS_CONNECTION_REUSE_ENABLED=1 (available from v2.463.0) to get the same behavior. If you still run v2 code, you should check this – it’s one of the cheapest latency improvements there is.

Lazy init for what not every invoke needs#

Not every resource should go into the Init phase. If you only actually need an expensive connection in one out of twenty calls, building it at module level lengthens every single cold start – including those that never use it. This is where lazy init pays off: the resource is only created when it’s first actually needed and is then cached for the lifetime of the environment.

import { createPool, type Pool } from "./db";

let dbPromise: Promise<Pool> | undefined;

// created on first use, then cached for the environment's lifetime
const getDb = (): Promise<Pool> => (dbPromise ??= createPool());

export const handler = async (event: { needsDb: boolean }) => {
  if (!event.needsDb) {
    return { statusCode: 204 };
  }

  const db = await getDb();
  const rows = await db.query("SELECT 1");
  return { statusCode: 200, body: JSON.stringify(rows) };
};

The ??= operator makes sure the pool is built only a single time. The first call that really needs it incurs a small delay – in return, the Init phase stays lean and fast, and all invokes that don’t need the pool pay nothing for it. It’s the balance between “everything up front” and “everything per call,” and the right boundary depends on how often a resource is actually needed.

The third lever: memory is also CPU#

This is the knob that is underestimated the most because it has a misleading name. The configuration value is called MemorySize and can be set between 128 MB and 10,240 MB in 1 MB steps. But it doesn’t only control memory: CPU is allocated linearly in proportion to memory. At exactly 1,769 MB, your function gets one full vCPU; at the top end, up to 6 vCPUs are available.

That has immediate consequences. A function that does CPU-bound work – parsing JSON, cryptography, data preparation – gets faster with more memory, even though it doesn’t need the extra memory at all. And interestingly, this also applies to the cold start itself: loading and initializing the runtime is CPU work that benefits from more allocated computing power.

The mistake I see most often here is one of two extremes. Either the function stays at 128 MB to save money and is therefore unnecessarily slow under CPU load – or someone blindly cranks memory up to a high level in the hope that “more helps more.” Both are guessing. More memory costs more per millisecond, and beyond a certain point it brings no latency gain because the function is no longer CPU-bound but I/O-bound. Then you’re waiting for a database or an external API, and no extra vCPU helps with that.

The only serious answer is measuring. For exactly this purpose, there is the open-source tool AWS Lambda Power Tuning, which tries your function with different memory values and plots cost against latency. Instead of guessing, you see the actual curve for your specific workload and pick the point that fits your goal.

The fourth lever: Provisioned Concurrency for predictable latency#

Sometimes shortening the cold start isn’t enough – you want to avoid it entirely for certain functions. That’s the case for latency-critical, well-predictable loads: a function behind a login flow, an endpoint that gets a predictable load peak at nine o’clock on weekdays.

That’s what Provisioned Concurrency is for. Using ProvisionedConcurrentExecutions, you configure a number of environments that Lambda keeps permanently pre-initialized – with the Init phase already run. Calls then hit a warm environment without a cold start occurring. The important detail is that Provisioned Concurrency is configured on an alias or a specific version, not on $LATEST. If you also want to adapt these environments to a fluctuating load, you can scale them dynamically with Application Auto Scaling.

That costs real money, though, and independently of load: pre-provisioned environments are billed even when they sit idle. Provisioned Concurrency is therefore not a cure-all you lay over every function, but a targeted tool for the few places where predictable, low latency justifies the surcharge. For most background and event-driven functions, the occasional cold start is completely uncritical – there, Provisioned Concurrency would be money thrown away.

A side note, because the question often comes up in reviews: for Java, there has also been Lambda SnapStart since the end of 2022, which works from a snapshot of the initialized environment. For Node.js, it’s currently not an option – SnapStart is Java-only at this point. For our TypeScript functions, the levers above remain the relevant ones.

Measure, don’t guess#

The common thread through all these levers is the same: without measurement, every optimization remains a gut feeling. Fortunately, Lambda delivers the decisive numbers for free. Every invoke writes a REPORT line to the CloudWatch logs, and for a cold start it contains an additional field:

{
  "Duration": "42.11 ms",
  "BilledDuration": "43 ms",
  "MaxMemoryUsed": "98 MB",
  "InitDuration": "312.44 ms"
}

The InitDuration field appears only on a cold start – it is exactly the time for the download, runtime init and your function init code. If you find this line in the logs, you know two things: first, this was a cold start. Second, the number shows you how expensive your Init phase really is. If InitDuration climbs toward seconds, that’s a clear signal that too much is happening at module level.

If you want to look deeper, enable AWS X-Ray. There, initialization shows up as its own subsegment in the trace, cleanly separated from handler time. This is especially valuable when you want to find out whether your latency comes from the cold start or from a slow downstream call in the handler – two completely different problems that can’t be told apart in the plain total.

The levers at a glance#

If I had to summarize the concrete knobs, I’d end up with a compact list that I like to use as a checklist in reviews:

  • Keep the bundle small with esbuild and tree shaking; mark the AWS SDK available in the runtime as external.
  • Use few dependencies and keep the import graph deliberately lean.
  • Create reusable clients and connections outside the handler and reuse them across warm invokes.
  • Lazy init for expensive, rarely used resources so the Init phase stays lean.
  • Understand memory as a CPU dial and tune it empirically – with Lambda Power Tuning instead of gut feeling.
  • Use Provisioned Concurrency selectively for the few latency-critical, predictable functions.
  • Measure Init Duration in CloudWatch and X-Ray instead of guessing.

Conclusion#

Cold starts are not a flaw but a property of Lambda’s execution model – and that is exactly why they can be controlled once you understand the model. The Init phase runs once per execution environment, warm invokes skip it, and almost everything you can adjust about latency depends on how heavy or light you make this Init phase.

In practice, there’s no single big solution here. What works is an interplay of small, understandable decisions: a lean bundle, clients in the right place, a sensibly chosen memory value and – only where it really pays off – Provisioned Concurrency. And above all of it is always measurement. If you keep an eye on Init Duration, you turn the vague “sometimes slow” into a number you can work on. That is the real gain: a gut feeling becomes an engineering problem with clear levers.

In the bigger picture, Lambda remains just one building block. API, persistence, identity, asynchronous processing and observability have to be considered together; a fast function alone doesn’t make a resilient system.

Further reading#

Revised and translated from German with the help of AI.