What is a Lambda cold start and how can you reduce it?
A cold start is the extra delay when Lambda has to create a new execution environment and load your code before running it. Reduce it with smaller packages, lighter runtimes, less work outside the handler, or provisioned concurrency.
Explain it simply
A cold start is like opening a tea stall in the morning. The first customer waits while you light the stove and boil water. After that, tea is served fast as long as the stove stays on. If no one comes for a while the stove is switched off and the next customer waits again.
Example
import boto3
# Runs once per new environment (cold start), then reused
s3 = boto3.client("s3")
def handler(event, context):
# Runs on every invocation
return {"statusCode": 200}During a cold start Lambda downloads the code, starts the runtime and runs initialisation code outside the handler. Later invocations that reuse the same environment are "warm" and skip all that.
Ways to reduce it:
- Keep deployment packages small and import only what you need.
- Pick a fast-starting runtime. Java and .NET usually start slower than Node.js or Python.
- Create SDK clients and connections outside the handler so warm calls reuse them.
- Use provisioned concurrency to keep environments initialised (costs extra).
- Avoid attaching to a VPC unless you need to.
What to say in the interview
"A cold start happens when Lambda must build a new execution environment. I keep packages small, initialise clients outside the handler, and use provisioned concurrency for latency-sensitive paths. For background jobs a cold start rarely matters."
Common mistakes
- Putting expensive initialisation inside the handler so it runs every time.