Lambda has a much-discussed feature: cold starts. It’s the “price” sometimes paid for the on-demand model from subchapter 14.1. In this subchapter, you’ll understand what they are, why they happen, and what you can do to reduce their impact.

What is a cold start

Remember how Lambda works: your code is not always running, but AWS starts it up when an event arrives. Well, that startup has two scenarios:

  • Cold start: it’s the first time the function is invoked (or after a while without use). AWS has to prepare the environment from scratch: reserve resources, load your code, initialize the runtime (Python, Java...), load libraries. This adds an extra delay, usually a few hundred milliseconds to a couple of seconds.

  • Warm start: if the function was used recently, AWS reuses the environment that was already prepared. Here there’s no preparation delay: the function responds instantly.

First invocation (cold):
  [prepare environment]──[load code and libraries]──[execute] 
  └──── extra delay (cold start) ────┘

Subsequent invocations (warm):
  [execute]   ← environment already ready, no delay

Analogy: a cold start is like starting a car on a winter morning: the cold engine takes a bit to get going. Once it’s running (warm), you start and accelerate instantly. If you leave it parked for a while, it cools down again.

Why it matters (and when it doesn’t)

The impact of cold starts depends on the type of application:

  • It matters in interactive applications where the user expects a quick response (an API responding to a mobile app). A 1-2 second delay on the first request can be noticeable.
  • It doesn’t matter for background or asynchronous tasks (processing an uploaded file, draining a queue). If the task takes a few extra seconds to start, nobody is affected.

It also depends on the language: lightweight runtimes like Python or Node.js have short cold starts; heavier runtimes like Java or .NET start up more slowly (though there are techniques to improve this).

Strategies to reduce cold starts

  1. Provisioned Concurrency

This is the most direct solution: you tell AWS to keep a number of environments always warm and ready, waiting for requests. Thus, those invocations never suffer a cold start.

Provisioned Concurrency = 5
  → AWS keeps 5 environments always warm
  → the first 5 simultaneous requests respond instantly

It’s like having several cars with the engine already running in the parking lot, ready to go. The trade-off: you pay to keep them warm (even if unused), so it’s used when first-request latency is critical.

  1. Keep the function “warm” with periodic invocations

A simple technique: schedule an invocation every few minutes (with CloudWatch, see subchapter 14.2) so the environment doesn’t cool down. It’s a cheap trick, though less reliable than provisioned concurrency, especially if there are many simultaneous requests.

  1. Reduce package size

The less code and libraries AWS has to load, the faster the startup. So it’s advisable to:

  • Include only the libraries you actually use (see subchapter 14.3).
  • Use layers to avoid inflating the package.
  • Avoid unnecessary heavy dependencies.

  1. Choose a lightweight runtime

If cold start is critical and you can choose the language, Python or Node.js usually start up faster than Java or .NET. (For Java there are specific optimizations, but by default it’s heavier.)

  1. Properly initialize code

What you put outside the handler (connections, configuration) runs only once per environment and is reused in warm starts. Using this well avoids repeating costly work on every invocation:

import boto3
# This runs ONCE when preparing the environment (reused when warm)
client = boto3.client("dynamodb")

def handler(event, context):
    # This runs on EVERY invocation
    return client.get_item(...)

Summary table

Strategy What it does Trade-off
Provisioned Concurrency Keeps environments always warm You pay to keep them, even if unused
Periodic invocations “Wakes up” the function every X minutes Cheap trick, less reliable
Reduce package size Less code/libraries to load Requires discipline with dependencies
Lightweight runtime Python/Node start up faster Depends on your language choice
Initialize outside handler Reuses connections between invocations Good practice, no cost

Should I obsess over this?

Not at first. For most applications, occasional cold starts are perfectly manageable, and runtimes like Python or Node make them very bearable. Worry about them only if you have an interactive application sensitive to latency and you measure that cold starts bother users. Then apply provisioned concurrency and the other strategies. Remember: optimizing before you have a measured problem is usually wasted time.

What you should remember

  • A cold start is the extra delay the first time a Lambda is invoked (or after a while without use), because AWS prepares the environment from scratch. In warm starts that environment is reused and there’s no delay.
  • It matters in interactive applications sensitive to latency; it doesn’t matter in background tasks. Lightweight runtimes (Python, Node) start up faster.
  • Strategies: Provisioned Concurrency (environments always warm), periodic invocations, reduce package size, lightweight runtime, and initialize connections outside the handler to reuse them.
  • Don’t obsess at first: optimize cold starts only if you measure that they truly affect your users.

In the last subchapter of the chapter, we’ll look at Lambda’s limits and antipatterns: when Lambda is not the right tool.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved