skip to content

In a function's logs and traces, how can an operator tell which individual invocations were cold starts?

level: juniorimportance: should knowfreq 48%

answer

  1. the platform's own init record
  2. code that runs once per instance
  3. a flag set in init, cleared after
  4. gap between init end and invocation

basics

~20 s

Use the init or startup duration most function platforms record when an invocation starts a new instance, and log your own cold flag: a variable set during init and cleared after that instance's first invocation.

solid answer

~40 s

Two sources. The platform's own: on many microVM-based platforms the per-invocation summary log line or trace carries an init duration only when that invocation started a new instance, while a container-instance platform reports startup latency per instance. Then my own tag, which is portable: at module scope, which runs once per instance, I set `coldStart = true`, an `instanceId` and an init timestamp; the handler logs the flag and the version on every invocation and sets it to `false` after the first. I also log the gap between the end of init and the start of the invocation, because an instance initialised ahead of traffic by pre-provisioned capacity also shows the flag on its first invocation, yet no user waited. Flag set and gap near zero means a request-path cold start.

go deeper

for a junior

Recall that module-scope code runs once per instance, so a flag set there and cleared in the handler marks exactly one invocation per instance as cold.

for a middle

Explain where each isolation model surfaces init: a per-invocation field on microVM platforms, a per-instance startup metric on container-instance platforms, often nothing on isolates.

for a senior

Show you separate request-path cold starts from pre-initialised instances with the init-to-invocation gap, and that you cross-check your flag against the platform's own record.

for a principal

Argue for a fleet-wide logging convention, the same cold flag, version and instance id on every function, so cold-start data is comparable across teams and providers.

## Why label each invocation An operator cannot tune what they cannot count. A **cold start** is an invocation that waited for the platform to create and initialise a new instance of the function (the sandbox, the runtime and your own module-level setup) before the handler ran. A **warm** invocation reused an instance that was already initialised. A dashboard of aggregate duration blends the two populations, so the first job is to label every single invocation as cold or warm, in its own log line and its own trace. Once each invocation carries that label, every later question (how many, after which release, costing how much latency) becomes a query instead of a guess. ## What the platform already records Most function platforms expose some signal of initialisation, but where it appears depends on the isolation model: | Isolation model | Where init usually shows up | Granularity | |---|---|---| | microVM-based function platform | an init or startup duration in the per-invocation summary log line or metric, present only when that invocation started a new instance | per invocation | | container-instance platform that sends many requests to one instance | a container startup latency metric and startup log lines | per instance, not per request | | isolate-based edge runtime | often nothing per request, because isolate creation is short and rarely surfaced | usually none | Where the platform's tracing service is enabled, it often draws an **initialisation segment** ahead of the handler segment on cold invocations. Read all of this as *usually*, not *always*: the field names, whether the signal exists at all and whether init is billed differ between providers, so confirm against your own platform before you build a dashboard on it. ## Tagging cold invocations yourself The platform signal is a good start, but your own label is portable across providers and lands in the same structured log line as everything else you record about the request. 1. **Set a flag during init.** Code at module scope runs once per instance, during the init phase. Set `coldStart = true` there, generate an `instanceId`, and record `initStartedAt` and `initEndedAt`. 2. **Read and clear it in the handler.** On each invocation, copy the flag into the structured log line and onto the request's root span as an attribute, then set it to `false`. Only the first invocation on that instance sees `true`. 3. **Record the version.** Log the version or alias the instance runs, so cold invocations can be grouped by release. 4. **Record the gap.** Log the time between `initEndedAt` and the start of this invocation. The next section explains why this field matters. ```pseudocode // module scope: runs once, when the instance initialises coldStart = true instanceId = newRandomId() initStartedAt = now() loadDependencies() initEndedAt = now() handler(event): log.info({ requestId: event.requestId, coldStart: coldStart, instanceId: instanceId, version: platformVersion(), gapSinceInit: now() - initEndedAt }) coldStart = false return process(event) ``` ## The pre-initialised instance trap Some instances are initialised before any request exists: **pre-provisioned capacity**, a **minimum instance count**, or a platform that starts instances ahead of predicted traffic. Their first invocation carries `coldStart = true`, yet no user waited for init. Counting those as cold overstates the problem, and it will make a warm-capacity setting look as if it did nothing. The gap field resolves it. A **request-path cold start** has a gap close to zero, because init finished just before the handler began. A pre-initialised instance shows a gap of seconds or minutes. Classify on both fields together: flag set *and* gap small means a user waited. A second trap sits on a **container-instance platform that sends many requests to one instance**. Several requests can queue behind one starting instance, but the flag marks only the first one the instance handles. There, record when the instance became ready and compare it with each request's receive time at the front door. ## Using the labels With a label on every invocation, the useful views are cheap to build: - **Cold-start count and ratio** per version, per route and per time window. - **A latency split**: duration percentiles for cold and warm invocations computed separately, so the **cold penalty** becomes a number you can track. - **Init attribution**: timings for each init step attached to the cold invocation's log line, so a slow dependency appears in the same record as the request it delayed. - **Instance lifetime**: invocations per `instanceId` show how long instances live before the platform recycles them. - **Cross-checks**: join the platform's own init record with your flag by request id. A platform init record without your flag means your tagging code did not run; your flag without a platform init record usually means a pre-initialised instance. Keep the label itself cheap: a boolean and a few numbers per line. Instrumentation that slows init would distort the very thing it measures.

  • On a container-instance platform that serves many requests per instance, why does a per-instance cold flag undercount affected requests?
    Several requests can queue while one new instance starts, and all of them wait for its startup, but the flag marks only the first request the instance handles. Record when the instance became ready and compare it with each request's receive time at the front door, such as the gateway or load balancer log. Any request received before readiness and served by that instance waited on the cold start, flag or not.
  • How do you reconcile the platform's own init record with your structured cold flag?
    Join them on the request id, which platforms commonly stamp on both their invocation summary and your handler's log lines. Disagreement is informative: a platform init record with no flag means your tagging code did not run on that path, and a flag with no platform init record usually means an instance initialised ahead of traffic.

  • A rising average duration proves cold starts rose; no per-invocation label is needed.
  • Setting a cold flag at the top of the handler marks only first invocations.
  • Every platform reports init duration on each cold request in the same field.
  • A pre-initialised instance's first invocation is a cold start a user waited on.
  • An environment variable set at deploy time can mark which instance is cold.