Explain AWS Lambda cold starts and how to mitigate them in production.
A cold start occurs when Lambda needs to initialize a new execution environment — download the code, start the runtime, run your initialization code. This adds 100ms-1s+ of latency on the first request.
Mitigation strategies:
- Provisioned Concurrency: Pre-warm a set number of Lambda execution environments. Eliminates cold starts for warmed instances (at extra cost).
- Minimize package size: Smaller deployment packages initialize faster.
- Use faster runtimes: Node.js and Python cold start faster than Java/C#.
- Move init code outside the handler: DB connections and SDK clients initialized at module level persist across invocations.
- Lambda SnapStart (Java): AWS-managed snapshot of initialized execution environment.