Stev_k
Stev_k

Reputation: 2126

Long cold start on AWS Lambda python function

I have converted a very simple flask function (i.e. two simple routes linking to a sql query and turning them into a table) to a Lambda function with Zappa. The deployed function is around 20MB. Because the traffic will be very low, I am not using any warming mechanism for my function.

The function only requires 128MB memory, and when run with this when all previous instances have been destroyed, the cold start is approximately 16 seconds.

This seems like a long time intuitively, and what I have read(e.g. here), which suggests that python functions not in a VPC have relatively low latency for cold starts.

If I add memory to the function, the cold start time seems to decrease linearly. Again this conflicts with what I have read (e.g. here as above) in terms of memory not being an issue for cold-start latency. This is my table of invocation times: enter image description here

Should I be surprised by these results or am I missing something?

Thanks

Stephen

Upvotes: 1

Views: 2424

Answers (2)

Shreyas Gaonkar
Shreyas Gaonkar

Reputation: 283

Before calling the handler function, the underlying CPU isn't throttled (see this re:invent video). Since the billed duration is decreasing as you are increasing the memory, something tells me that you might have written function definitions within the handler, which is naturally throttled as per the Memory allocated to the function, taking longer time.

Try describing all the functions, static variables outside the handler, and keep handler code minimal. This will ensure Lambda spends more time outside the handler with full CPU capacity before invoking the handler function where it is throttled.

Best way to get more idea about this is my profiling your function code using X-Ray segments to see where function is spending more time. This will paint a clearer picture if those are indeed cold starts, or just function taking longer.

Note: Cold start durations are not counted towards your function duration metrics but rather will show as "init duration" when you enable X-Ray tracing.

Upvotes: 2

BAD_SEED
BAD_SEED

Reputation: 5056

I'm not (so) surprised. Have in mind that under 1GB of RAM the CPU is single core and the CPU grows linearly with memory.

Try this tool for fine tuning of your Lambda memory/power and cost. If you don't want to raise your memory try using Provisioned Concurrency to cut coldstarts.

PS: are you sure that those time are because of cold starts?

Upvotes: 2

Related Questions