DEV Community

Cover image for ๐’๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐‚๐จ๐ฅ๐ ๐’๐ญ๐š๐ซ๐ญ๐ฌ ๐ข๐ง ๐€๐–๐’ ๐’๐ž๐ซ๐ฏ๐ž๐ซ๐ฅ๐ž๐ฌ๐ฌ (๐‹๐š๐ฆ๐›๐๐š)
Shubham_Baghel
Shubham_Baghel

Posted on

๐’๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐‚๐จ๐ฅ๐ ๐’๐ญ๐š๐ซ๐ญ๐ฌ ๐ข๐ง ๐€๐–๐’ ๐’๐ž๐ซ๐ฏ๐ž๐ซ๐ฅ๐ž๐ฌ๐ฌ (๐‹๐š๐ฆ๐›๐๐š)

One of the most common performance challenge in serverless app is the Lambda cold start that small delay when AWS spins up a fresh execution environment after inactivity.

Some easy tricks to make Lambda functions start faster in production:

1.๐”๐ฌ๐ž ๐๐ซ๐จ๐ฏ๐ข๐ฌ๐ข๐จ๐ง๐ž๐ ๐‚๐จ๐ง๐œ๐ฎ๐ซ๐ซ๐ž๐ง๐œ๐ฒ

Keeps a set number of Lambda instances always warm.

AWS pre-initialise execution environments for you so no first-call delay.

Configure via Console, CLI, or CDK:
AWS Docs: AWS provisioned concurrency

Tip: Perfect for APIs or real-time workloads (e.g., login or payment endpoints).

2.๐Š๐ž๐ž๐ฉ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ ๐–๐š๐ซ๐ฆ ๐ฐ๐ข๐ญ๐ก ๐š ๐’๐œ๐ก๐ž๐๐ฎ๐ฅ๐ž๐ซ

Trigger your function periodically (every 5โ€“10 mins) to prevent it from going cold.

Use Amazon EventBridge or CloudWatch Schedule for simple pinging.

EventBridge Docs:EventBridge

Tip: Combine this with a lightweight health-check lambda endpoint (like /ping). This is useful in microservices setups for example, after a new deployment, you can quickly test and verify that all functions are warm and responding correctly.

3.๐Ž๐ฉ๐ญ๐ข๐ฆ๐ข๐ณ๐ž ๐ƒ๐ž๐ฉ๐ฅ๐จ๐ฒ๐ฆ๐ž๐ง๐ญ ๐๐š๐œ๐ค๐š๐ ๐ž ๐’๐ข๐ณ๐ž

The bigger your Lambda zip, the longer AWS takes to initialize it.

Bundle only whatโ€™s needed (exclude dev dependencies).

Use tools like esbuild, webpack, or AWS SAM build.

Tip: For Node.js avoid installing large libraries like aws-sdk (itโ€™s pre-included) and remove unused packages with library called depcheck.

4.๐‚๐ก๐จ๐จ๐ฌ๐ž ๐ญ๐ก๐ž ๐‘๐ข๐ ๐ก๐ญ ๐‘๐ฎ๐ง๐ญ๐ข๐ฆ๐ž

Cold start time depends heavily on the runtime.

Node.js and Python start up faster than Java or .NET.

AWS SnapStart Doc:AWS SnapStart

Tip: If you need Java, enable AWS Lambda SnapStart
to pre-initialise the function state.

5.๐๐ž ๐‚๐š๐ซ๐ž๐Ÿ๐ฎ๐ฅ ๐ฐ๐ข๐ญ๐ก ๐•๐๐‚ ๐‚๐จ๐ง๐Ÿ๐ข๐ ๐ฎ๐ซ๐š๐ญ๐ข๐จ๐ง

Putting Lambdas inside a VPC adds ENI (Elastic Network Interface) setup time.

Avoid VPC unless you truly need private resources (like RDS).

If required, use VPC endpoints or NAT optimization.
Best Practices for VPC Lambda Networking: VPC configuration

Tip: For DynamoDB, S3, or SNS we can go without a VPC entirely.

6.๐’๐ฉ๐ฅ๐ข๐ญ ๐‹๐š๐ซ๐ ๐ž ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ

Monolithic Lambdas = longer cold starts.

Break down your logic into multiple smaller Lambdas (micro-Lambdas).

Tip: This also improves deploy speed and observability.

In one project, our first API call took ~2.4s (cold Lambda in VPC).
After making a few changes like
โ€ข Removed VPC dependency
โ€ข Enabled provisioned concurrency (2 instances)
โ€ข Added an EventBridge warm-up every 10 mins

Cold start dropped to ~150ms consistently.

Top comments (0)