Dev Overflow Logo

Dev Overflow

Global search

Search across questions, answers, users and tags.

Loading...
save

AWS Lambda cold starts are killing my p99 — what actually helps?

clock icon

asked 2 months ago

message icon

2

eye icon

941

p50 is 40ms, p99 is 2.8 seconds. It is clearly cold starts. I have seen advice ranging from "use provisioned concurrency" to "rewrite in Go" and would like to know what is worth doing first.

2 Answers

In rough order of effort-to-payoff:

  1. Shrink the bundle. Cold start scales with how much code has to be loaded and parsed. Bundle with esbuild, tree-shake, and stop importing the entire AWS SDK when you need one client.
  2. Move work out of the handler. Anything at module scope runs once per container, not once per invocation — put client construction there.
  3. Do not open a VPC connection you don't need. VPC-attached Lambdas used to be the worst offender; it is much better now, but still non-zero.
  4. Provisioned concurrency, only after the above. It works, and you pay for it whether or not traffic arrives.

Rewriting in Go is real but it is the largest change for a gain you may not need once the bundle is 2MB instead of 40MB.

Check whether the p99 is cold starts at all before optimising — AWS/Lambda publishes InitDuration separately. I spent a week on bundle size once when the real cause was a downstream database connection timing out.

1

of 1

Write your answer here

Introduce the problem and expand on what you've put in the title.

Top Questions