debugjavascriptaws-lambdaMajorpending
Debug: AWS Lambda timeout or cold start issues
Viewed 0 times
lambdacold-starttimeoutprovisioned-concurrencymemory
Error Messages
Problem
Lambda function times out or has very slow cold starts, causing user-facing latency spikes.
Solution
Cold start and timeout optimization:
- Minimize package size (exclude dev deps, use layers)
- Use ARM64 (Graviton2) — faster startup, cheaper
- Avoid heavy frameworks (Express -> native handler)
- Use Provisioned Concurrency for critical paths
- Lazy-load: initialize DB connections outside handler
- Default timeout is 3 seconds — increase if needed
- Check if waiting for DB connection in cold start
- Move initialization outside handler:
// GOOD: Connection reused across invocations
const db = createConnection();
exports.handler = async (event) => {
return await db.query(...);
};
- Enable X-Ray tracing
- CloudWatch Insights:
filter @type = 'REPORT'
| stats avg(@duration), max(@duration), avg(@initDuration)
- Lambda CPU scales linearly with memory
- 256MB = slow, 1024MB = 2x faster, 1769MB = full vCPU
- More memory can be CHEAPER (faster = less billing duration)
- Provisioned Concurrency (cost)
- Or: scheduled ping every 5 minutes (hacky but works)
- Reduce cold start time:
- Minimize package size (exclude dev deps, use layers)
- Use ARM64 (Graviton2) — faster startup, cheaper
- Avoid heavy frameworks (Express -> native handler)
- Use Provisioned Concurrency for critical paths
- Lazy-load: initialize DB connections outside handler
- Fix timeout issues:
- Default timeout is 3 seconds — increase if needed
- Check if waiting for DB connection in cold start
- Move initialization outside handler:
// GOOD: Connection reused across invocations
const db = createConnection();
exports.handler = async (event) => {
return await db.query(...);
};
- Monitor and debug:
- Enable X-Ray tracing
- CloudWatch Insights:
filter @type = 'REPORT'
| stats avg(@duration), max(@duration), avg(@initDuration)
- Memory affects CPU:
- Lambda CPU scales linearly with memory
- 256MB = slow, 1024MB = 2x faster, 1769MB = full vCPU
- More memory can be CHEAPER (faster = less billing duration)
- Keep warm (avoid cold starts):
- Provisioned Concurrency (cost)
- Or: scheduled ping every 5 minutes (hacky but works)
Revisions (0)
No revisions yet.