← Workflows

阅读中文版本

Cold Start Mitigation Workflow

Measure the cold-start latency of a serverless function, identify whether it is import or init bound, and apply the right mitigation from provisioned concurrency to lazy imports.

intermediate~45 minserverlesscold-startperformance

Objective

Reduce the p99 cold-start latency of a serverless function below the SLO by targeting the dominant cost with the right mitigation.

Environment

  • A serverless platform that reports cold-start duration per invocation
  • A function with a measurable cold start above the SLO
  • A load generator that can force cold starts on demand

Steps

1. Measure the cold start

Force a cold start with the load generator, record the duration, and split it into import time and init time using the platform profile.

Expected state: A breakdown that says whether the cost is in module import or in handler initialization.

2. Target the dominant cost

If import-bound, lazy-import heavy modules; if init-bound, move work out of the handler or use provisioned concurrency.

Expected state: The mitigation matches the cost, so the fix actually moves the number.

3. Re-measure and verify

Force another cold start, confirm the p99 drops below the SLO, and check that warm invocations are unaffected.

Expected state: Cold-start p99 is below the SLO and warm latency is unchanged.

4. Lock it in

Add a cold-start regression test to CI that fails if the p99 rises above the SLO, and document the mitigation in the runbook.

Expected state: A future change that regresses the cold start fails CI before it ships.

Decision points

Provisioned concurrency or lazy imports?

  • Provisioned concurrency for predictable cost
  • Lazy imports for zero cost

Lazy imports first; provisioned concurrency when the init is irreducible and the traffic is predictable enough to pay for it.

How many cold starts to sample?

  • Five
  • Twenty

Twenty, so a single slow import does not skew the p99.

Expected outcome

Cold-start p99 is below the SLO, warm invocations are unaffected, and a CI test prevents regression.

Verification

Confirm the cold-start p99 is below the SLO across 20 forced cold starts, warm latency is unchanged, and the CI test passes.

Cleanup

Remove the load-generator artifacts, keep the regression test, and record the mitigation in the runbook for the next on-call.

Measure before you fix

A cold start has two costs: module import and handler init. The platform profile splits them. Fixing the wrong one moves nothing, so measure first.

serverless invoke --function api --log --profile cold

Match the mitigation to the cost

If the cost is import, lazy-import the heavy module so it loads only when used. If the cost is init, move the work out of the handler or pay for provisioned concurrency. The mitigation must match the cost.

Lock it in

Add a cold-start regression test to CI that forces a cold start and fails if the p99 rises above the SLO. A future change that adds a heavy import fails CI before it ships.

Verification

The cold-start p99 is below the SLO across 20 forced cold starts, warm latency is unchanged, and the CI test passes. Sample 20 so a single slow import does not skew the p99. Prefer lazy imports over provisioned concurrency when the init is reducible.