Finding Slow Code and Right-Sizing Lambda Memory
This lesson explains how profiling shows where an application spends its time and resources, and how right-sizing matches allocated capacity to what the application actually needs.
Learning outcomes
After completing this lesson, you will be able to:
- Choose the tool that shows the right level of detail for a performance problem.
- Explain how Lambda memory affects CPU, duration and cost, and how to find the best setting.
- Explain what load testing reveals before launch.
Finding slow code
Finding the cause of poor performance means narrowing down from the whole request to the operation that is slow, and each tool shows a different level of detail.
CloudWatch Lambda Insights shows how a function uses its resources on each invocation. High CPU time close to the duration means the function is CPU-bound and will benefit from more memory, which brings more CPU, whereas low CPU time with a long duration means it spends its time waiting for other services. X-Ray subsegments then show where that waiting happens: each call to DynamoDB, S3 or an HTTP API, and any section of code wrapped in a custom subsegment, appears with its own duration on the trace. Logs Insights shows how performance varies over time and across requests: functions such as pct(@duration, 99) over the Lambda REPORT lines reveal slow outliers that an average hides. The following table summarises the tools.
| Tool | What it shows |
|---|---|
| CloudWatch Lambda Insights | CPU, memory and network use per invocation, which shows whether a function is CPU-bound or waiting on I/O |
| X-Ray subsegments | The time taken by individual operations |
Logs Insights, e.g. pct(@duration, 99) | Latency outliers over time |
CPU-bound or waiting?
Right-sizing Lambda memory
Lambda allocates CPU in proportion to memory, so the memory setting controls both how much data a function can hold and how fast it computes. Because the price is memory multiplied by duration, a larger setting is not necessarily more expensive: if doubling the memory halves the duration, the cost stays the same and users get faster responses.
The REPORT line written after each invocation shows the configured memory size, the maximum memory used, the duration and, for cold starts, the init duration. If the maximum memory used is far below the setting and tests show that more memory does not shorten the duration, the function is over-provisioned and the setting can be lowered. If the duration falls sharply as memory increases, the function is CPU-bound and benefits from more memory. The following table summarises how to read the results.
| What you see | What to do |
|---|---|
Max Memory Used far below the setting, and more memory does not shorten duration | Lower memory to save cost |
| Duration drops sharply as memory increases | The function is CPU-bound; raise memory for more CPU |
filter @type = "REPORT"
| stats max(@memorySize / 1000 / 1000) as configuredMB,
max(@maxMemoryUsed / 1000 / 1000) as maxUsedMB,
avg(@billedDuration) as avgBilledMs,
pct(@duration, 99) as p99MsThe query above summarises the memory and duration of a function. The following list describes tools that help choose a memory setting.
- AWS Lambda Power Tuning runs a function at several memory sizes and plots cost against speed.
- CloudWatch Lambda Insights shows memory and CPU use over time, confirming that a lower setting leaves enough headroom.
Load testing
Load testing before launch, with Distributed Load Testing on AWS or open-source tools, reveals bottlenecks and throttling limits, and shows the concurrency and capacity settings the application really needs.
Right-sizing a function's memory
A function is configured with 3,008 MB "to be safe". Logs Insights on its REPORT lines shows maximum memory used is 180 MB and average duration 120 ms.
| Memory setting | Duration | Cost per million invocations (relative) |
|---|---|---|
| 3,008 MB | 120 ms | 100% |
| 1,024 MB | 130 ms | about 37% |
| 512 MB | 190 ms | about 27% |
| 256 MB | 420 ms | about 30%, and slower for users |
512 MB is the cheapest setting that keeps duration acceptable; below that, the function becomes CPU-bound and both latency and cost rise. Lambda Power Tuning produces exactly this table automatically by running the function at each setting.
Why can giving a Lambda function more memory lower its cost?
Show answer
CPU is allocated in proportion to memory, so a CPU-bound function runs faster. Cost is memory multiplied by duration, so a large enough drop in duration outweighs the higher price per millisecond.
Exam Tips
Remember for the exam
- To find which downstream call makes a request slow, add X-Ray subsegments around it.
- To find the best Lambda memory setting, use Lambda Power Tuning.
- If memory is far above what the function uses and more memory brings no speed gain, lower it.
- To discover throttling limits before launch, load test the application.