Serverless cold starts: why the first request is slow
Short answer: A cold start is the extra delay when a serverless platform has to create a fresh environment for your function, load your code and run its start-up logic before handling a request. It's why the first call after a quiet period can take noticeably longer than the rest. Reduce it by keeping deployment packages small, doing less work at start-up, choosing a lighter runtime where it matters, and, for latency-sensitive endpoints, paying for pre-warmed capacity such as provisioned concurrency or using snapshot-based start-up features.
What actually happens on a cold start
Serverless functions run in short-lived environments that the platform creates on demand. AWS's documentation on the Lambda execution environment lifecycle describes an Init phase, in which Lambda sets up the environment, starts the runtime and runs your function's initialisation code, before the Invoke phase that handles the request. When a request arrives and no ready environment exists, it waits for Init: that's the cold start.
After the request, the environment usually stays around for a while and handles later requests without repeating Init. Those are warm starts. When traffic drops, environments are eventually removed, and the next request is cold again. Bursts of traffic can also cause cold starts, because the platform creates new environments in parallel to keep up.
Other serverless platforms work in broadly similar ways, with their own names and settings.
Why it matters for a business app
For background tasks, an extra second rarely matters. It hurts in places like:
- The first page load of the morning on an internal tool or booking site with quiet nights.
- Checkout and payment steps, where a slow response makes customers click twice.
- Chained functions, where one request calls several functions and each may be cold.
- Webhook endpoints with tight response deadlines from the sender.
Measure before optimising: look at your platform's metrics or traces for the initialisation duration and how often it occurs on the endpoints customers use.
Make initialisation cheaper
Most cold start time is usually your own start-up work, so start there.
Shrink the package. Large deployment packages take longer to load. Remove unused dependencies, avoid bundling development tools, and use a bundler that drops unused code for JavaScript functions.
Defer what you can. Code outside your handler runs during Init. Loading every SDK client, reading large config files or warming caches at start-up makes every cold start slower. AWS's provisioned concurrency documentation includes an example of initialising a client only when a condition requires it, so the cost is only paid when needed.
But reuse what you create. Connections and clients created once and kept in a variable outside the handler are reused on warm invocations, which is good. The balance is: initialise what nearly every request needs, defer what only some requests need.
Watch database connections. Opening a new database connection on each cold start is slow and can exhaust connection limits during bursts. A connection pooler or proxy in front of the database helps.
Choose the runtime with cold starts in mind
Runtimes differ in start-up cost. Interpreted runtimes with small packages often start quickly; runtimes that need a heavier virtual machine or framework initialisation can take longer, though platform features have narrowed the gap. If a single endpoint is very latency-sensitive, it may be worth writing it in a lighter runtime even if the rest of the app uses something else. Measure with your own code rather than relying on general comparisons.
Pay for warm capacity where it counts
Platforms offer ways to keep environments ready:
- Provisioned concurrency. AWS describes it as a number of pre-initialised execution environments ready to respond immediately to requests. Its documentation states that configuring it incurs additional charges.
- Snapshot-based start-up. Lambda SnapStart, for supported runtimes, takes a snapshot of the memory and disk state of an initialised environment when you publish a version, and restores from it instead of running full initialisation, according to AWS's documentation.
- Minimum instances on other platforms keep a set number of instances running.
These cost money even when idle, so apply them only to the functions where users feel the delay, and schedule them for business hours if your platform allows. Check current pricing first.
Avoid "keep-warm" hacks as a strategy
A common trick is pinging a function every few minutes to keep it warm. It can help a little for a single quiet function, but it doesn't help with bursts, it keeps only one environment warm, and it adds noise to your metrics. Platform-supported options are more predictable.
Rethink the architecture where needed
If cold starts keep hurting a particular flow:
- Combine chained functions so a request touches fewer cold environments.
- Move slow, non-urgent work to a queue, so the user-facing function returns quickly.
- Consider running the latency-critical part on an always-on container or server instead.
Serverless doesn't have to be all or nothing.
A worked example
Take a hypothetical Darwin tour operator whose booking API ran as several serverless functions. The first booking of each morning often took several seconds, and customers sometimes submitted twice. Tracing showed most of the delay was initialisation: a large package with an unused PDF library and three SDK clients created at start-up. The developer trimmed the package, deferred the PDF and email clients to the functions that used them, added a connection proxy for the database and enabled pre-warmed capacity for the checkout function during booking hours. First-request delays shrank, and duplicate bookings stopped.
FAQ
Do cold starts affect SEO?
Only if crawlers often hit slow first requests on public pages. Static pages served from a CDN avoid the issue.
How long does an environment stay warm?
Platforms don't generally guarantee it, and it varies. Design as though any request might be cold.
Does more memory reduce cold starts?
On some platforms, more memory also brings more CPU, which can speed initialisation. Test it, as it changes cost.
If your serverless app is slow on first requests, ask us to trace where the time goes.