Skip to content

Lesson 2 of 9 · 20m

Cold Starts and Latency Engineering

Cold starts are a latency budget problem — measure init time, shrink deployment artifacts, choose runtimes deliberately, and warm only what matters.

A cold start happens when Lambda must create a new execution environment before your handler can run. Teams either panic about it or ignore it. The better question is narrower: does initialization latency hurt a user-visible path, and what is the cheapest way to remove that specific pain?

Cold start is time before INVOKE

event arrives
  ├─ no warm environment?
  │    ├─ download package or image
  │    ├─ start runtime
  │    ├─ execute module-level code
  │    └─ initialize extensions and clients
  └─ invoke handler

A burst can create many cold starts because many environments are created at once. CloudWatch exposes the cost in REPORT lines:

REPORT RequestId: 7c9e... Duration: 83.41 ms Billed Duration: 84 ms
Init Duration: 421.18 ms Memory Size: 512 MB Max Memory Used: 108 MB

Init Duration happened before your handler. Measure it per runtime and deployment; do not argue from anecdotes.

Package size and imports are the big levers

A bloated artifact slows download and filesystem setup, then usually makes module-level imports expensive. Inspect what you ship:

du -sh function.zip package/
python -m pip install -r requirements.txt --target package
find package -type f | wc -l

Common offenders are scientific stacks, test files, copied node_modules, and importing an entire SDK when one client is enough. Lazy imports are legitimate for rare paths:

def handler(event, context):
    if event.get("kind") == "csv":
        import pandas as pd  # paid only on this path
        return summarize_csv(event)

Use this selectively. Universal setup still belongs at module scope; rare expensive dependencies can be deferred.

Runtime and memory are empirical choices

Python and Node.js generally initialize quickly. JVM and .NET can be excellent steady-state runtimes but need more care unless you use optimizations such as SnapStart or Native AOT. Go and Rust start quickly, but team competence matters more than benchmark folklore.

Lambda also scales CPU with memory. Doubling memory can shorten duration enough to be cost-neutral, sometimes cheaper. Benchmark powers such as 256, 512, 1024, and 1769 MB rather than guessing; aws-lambda-power-tuning exists because this is empirical.

Warm deliberately

Provisioned concurrency keeps environments initialized:

ProvisionedConcurrencyConfig:
  ProvisionedConcurrentExecutions: 10

Use it when the path is synchronous, latency spikes have business impact, and load tests show how much concurrency you need. Do not use it to hide a careless deployment package.

Field notes

  • Optimize p95 and p99, not average latency; averages hide burst behavior users remember.
  • VPC networking used to dominate cold-start stories. It is much better now, but still test the real architecture.
  • Queued work often tolerates cold starts because the user is already asynchronous.
  • If every function is provisioned all the time, you may have rebuilt servers with less control.

Was this lesson helpful?

Sign in to rate