Cold starts are one of the most discussed and least understood performance topics in serverless .NET. Many of the usual tips either address something you cannot control or were true years ago and no longer are. This guide separates the levers that move the number from the ones that mostly add complexity.
What a cold start actually contains
When Lambda has no warm execution environment available, it creates one. That includes downloading your code, starting the runtime, and running your initialization code. On .NET, the part you can influence most is the runtime start and your own startup logic: loading assemblies, JIT-compiling code, building the dependency injection container and creating SDK clients.
The easiest way to see it is the REPORT line Lambda writes to CloudWatch Logs for each invocation. Cold invocations include an Init Duration field. Query that field in CloudWatch Logs Insights to get p50, p95 and p99 for your own function before changing anything.
The levers, from most to least effective
| Lever | What it does | Trade-off |
|---|---|---|
| Native AOT | Compiles to native code ahead of time, removing most JIT and runtime start cost | Reflection-heavy libraries need care; custom runtime build |
| SnapStart | Restores a snapshot of an initialized environment instead of initializing from scratch | Code must tolerate being restored (unique IDs, connections, random seeds) |
| More memory | Gives the function more CPU, which speeds up initialization | Higher cost per millisecond; test to find the sweet spot |
| ReadyToRun | Pre-compiles IL so less JIT work happens at startup | Larger package; gain depends on your code |
| Leaner startup code | Fewer assemblies loaded and fewer objects built before the first request | Requires reviewing what your startup really needs |
| Provisioned concurrency | Keeps environments initialized in advance | You pay for capacity whether or not traffic arrives |
Capabilities and supported runtimes change, so confirm the current options in the AWS documentation before building on them. In particular, .NET 6 is no longer a supported managed runtime on Lambda; new work should target a currently supported .NET version.
Memory is a cold start lever, not just a cost setting
Lambda allocates CPU in proportion to memory. A function with little memory also has little CPU, and the initialization phase, which is mostly CPU work for .NET, runs slower. Raising memory often shortens init time noticeably, and since cost is memory multiplied by duration, a faster function can cost about the same or even less. The maximum is 10,240 MB.
The practical approach is to measure rather than guess. The open source AWS Lambda Power Tuning tool runs your function at several memory sizes and plots cost against duration, so you can pick a configuration from data.
Initialization: do the expensive work once
Lambda reuses an execution environment for many invocations, so anything created outside the handler is built once per environment. SDK clients are the classic example.
public class Function
{
// Created once per execution environment, reused by every invocation
private static readonly AmazonS3Client S3Client = new AmazonS3Client();
public async Task<APIGatewayProxyResponse> FunctionHandler(
APIGatewayProxyRequest request, ILambdaContext context)
{
var response = await S3Client.GetObjectAsync("my-bucket", "key");
// handle response
}
}
For things only some requests need, defer the cost with Lazy<T> so the other requests never pay for it:
private static readonly Lazy<IAmazonSecretsManager> Secrets =
new Lazy<IAmazonSecretsManager>(() => new AmazonSecretsManagerClient());
A note on databases: do not keep a single static SqlConnection for the lifetime of the environment. ADO.NET already pools connections, so create and dispose a connection per use and let the pool handle reuse.
VPC: less of a villain than it used to be
Older advice says VPC-attached functions suffer multi-second cold starts because a network interface is created for each environment. Since AWS changed how Lambda networking into a VPC works in 2019, network interfaces are created when the function is configured, not on each cold start, so that penalty largely disappeared. Attaching a function to a VPC is no longer a reason to expect several extra seconds. Keep it off a function that does not need private resources, but do not treat it as the main suspect.
Ideas that sound good but rarely pay off
- Scheduled warm-up pings: a single ping keeps one environment warm. Lambda scales out, so concurrent traffic still creates new environments that start cold. If you need guaranteed warm capacity, use provisioned concurrency.
- Splitting one function into many: each smaller function usually still references most of the same dependencies, so you get more functions with similar start behavior and more to deploy and monitor.
- Using layers to speed up starts: layers help share code and manage package structure. The same assemblies still load and compile, so they are not a cold start fix.
- Trimming package size for its own sake: download time is only part of a cold start. Remove dependencies you do not need because they add load and JIT work, and verify the effect rather than assuming it.
A sensible order of work
- Measure
Init Durationand compare it with warm latency. If cold starts are rare and within your latency target, stop. - Run Power Tuning to choose a memory size.
- Target a currently supported .NET runtime and consider Native AOT or SnapStart if cold starts still matter.
- Review startup: remove unused packages, create SDK clients statically, defer rarely needed ones with
Lazy<T>. - Try ReadyToRun if JIT time shows up as a significant share of init.
- Only then consider provisioned concurrency, sized to the minimum that meets your latency goal.
The goal is not zero cold starts. It is cold starts that are fast and rare enough that users and latency targets do not suffer. Change one thing at a time and keep the numbers.
Comments
Post a Comment