λ Lambda Optimization
Lambda has unique optimization knobs — concurrency types, memory settings, cold starts, and automated tuning tools. Understanding these well is essential for both the exam and real-world serverless applications.
🔑 Covers: Cold Starts · Concurrency Types · Provisioned Concurrency · Power Tuning · Memory OptimizationCold Starts — The #1 Lambda Latency Problem
When a Lambda function is invoked, AWS needs to set up an execution environment. If there's an existing warm environment available, the function starts immediately. If not, AWS must create a new one — this is a cold start.
⏱️ Cold Start Duration by Runtime
| Runtime | Typical Cold Start | Notes |
|---|---|---|
| Python | 50–300ms | Fast — interpreted, minimal JVM-like initialization |
| Node.js | 50–300ms | Fast — similar to Python |
| Java | 500ms–3 seconds | Slow — JVM startup is heavy. Use GraalVM native image to reduce. |
| .NET (C#) | 400ms–2 seconds | Moderate — .NET CLR initialization |
💡 Cold Start Reduction Strategies
- Provisioned Concurrency: Best solution — pre-initializes environments, zero cold starts (see below)
- Keep functions warm: Use EventBridge to ping the function every few minutes (workaround, not recommended for production)
- Reduce package size: Smaller deployment packages = faster download = faster cold start
- Use faster runtimes: Python/Node instead of Java for latency-sensitive functions
- Minimize init code: Move initialization code outside handler when possible (it runs once per environment)
- Avoid large dependencies: Only import what you need
Lambda Concurrency — Three Types You Must Know
Concurrency in Lambda means: how many instances of your function are running at the same time. Lambda scales horizontally — each concurrent request gets its own execution environment.
1️⃣ Unreserved Concurrency
The default. A function uses whatever concurrency is available from the shared account pool. No guarantees — if other functions eat up all the concurrency, this function gets throttled.
2️⃣ Reserved Concurrency
Solution: Set Reserved Concurrency = 800 on the payment Lambda (guarantees it always has capacity) and set Reserved Concurrency = 50 on the report Lambda (caps it from consuming too much).
Result: Payment Lambda always has 800 slots. Report Lambda can use at most 50. Everyone gets their share.
Cost: No extra charge for Reserved Concurrency — you only pay for actual function execution.
3️⃣ Provisioned Concurrency
✅ Provisioned Concurrency — Good for
- Latency-sensitive applications (API responses, real-time processing)
- Functions with heavy initialization (Java, loading large ML models)
- Predictable traffic patterns (enable during business hours, disable at night)
- When consistent response times are required (user-facing APIs)
⚠️ Provisioned Concurrency — Watch out
- You pay for provisioned concurrency even when the function isn't invoked
- Cost = (provisioned count × duration × per-GB-second rate)
- Not needed for batch jobs, background tasks, or tolerant of occasional latency spikes
- Use Application Auto Scaling to scale provisioned concurrency up/down with traffic
Provisioned Concurrency: Pre-warms environments. Used to ELIMINATE cold starts. DOES cost extra (even when idle). Provides consistent low-latency responses.
Burst Concurrency — How Lambda Scales
When traffic suddenly spikes, Lambda needs to scale up quickly. But it can't scale infinitely and instantly — there are limits.
If requests come in faster than Lambda can scale, excess requests are throttled (return HTTP 429). For synchronous invocations (API Gateway), the caller receives a throttle error immediately. For asynchronous invocations (SQS, SNS), Lambda retries automatically.
Lambda Power Tuning — Find the Optimal Memory
Lambda memory can be set from 128MB to 10,240MB (10GB). More memory = more CPU = faster execution. But more memory = higher per-ms cost. The optimal setting depends on your specific function. Lambda Power Tuning finds it for you automatically.
🛠️ How Lambda Power Tuning Works
Deploy Power Tuning Tool
Lambda Power Tuning is an open-source AWS Step Functions state machine. Deploy it to your account (takes 2 minutes via SAM/CloudFormation).
Run the State Machine
Provide your function ARN and a list of memory sizes to test (e.g., 128, 256, 512, 1024, 2048, 3008 MB). Also provide a sample event payload.
Automated Testing
Power Tuning invokes your function multiple times at each memory configuration. It measures execution time and calculates cost (time × memory price).
View Results
Returns a graph showing cost vs performance for each memory setting. You choose: optimize for speed (lowest latency), cost (cheapest), or balanced. Often more memory = faster AND cheaper because the shorter duration more than offsets the higher per-ms cost.
128MB: Takes 3400ms → costs $0.0000712 per invocation
512MB: Takes 820ms → costs $0.0000344 per invocation ← cheaper AND faster!
2048MB: Takes 340ms → costs $0.0000286 per invocation ← fastest and cheapest!
Conclusion: The 128MB setting is 52% more expensive AND 10x slower. Increasing memory helps both performance and cost.
Lambda Best Practices — Developer Exam Focus
📦 Initialize Outside the Handler
Code at the module level (outside the handler function) runs once when the execution environment is created — not on every invocation. Use this for expensive initialization:
🔑 Key Lambda Optimization Tips
| Optimization | What to Do | Benefit |
|---|---|---|
| SDK client initialization | Move outside handler (module level) | Reused across invocations → saves 50-200ms |
| Database connections | Use RDS Proxy for connection pooling | Prevents connection exhaustion during Lambda scaling |
| Package size | Only bundle required libraries, use Lambda Layers for common deps | Faster cold starts (less to download) |
| Memory size | Use Lambda Power Tuning to find optimal | Can reduce both cost and latency simultaneously |
| Cold starts (critical workloads) | Enable Provisioned Concurrency | Zero cold start latency |
| Timeout | Set appropriate timeout (not too long) | Faster failure detection, cost control |
| Concurrent scale protection | Set Reserved Concurrency limits | Protects account-wide concurrency pool |