Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase functions slow cold start time

I read here endpoint spin-up is supposed to be transparent, which I assume means cold start times should not differ from regular execution times. Is this still the case? We are getting extremely slow and unusable cold start times - around 16 seconds - across all endpoints.

Cold start: Function execution took 16172 ms, finished with status code: 200 After:Function execution took 1002 ms, finished with status code: 304

Is this expected behaviour and what could be causing it?

like image 316
sqwerty Avatar asked Jan 16 '18 11:01

sqwerty


People also ask

What is cold start in Firebase?

Note: Several of the recommendations in this document center around what is known as a cold start. Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start. Cold starts can take significant amounts of time to complete.

How fast is Firebase cloud functions?

The function itself takes about 400ms, so that's alright. But sometimes the functions take a very long time (around 8 seconds), while the entry was already added to the queue.

Can I use cloud functions Firebase for free?

Cloud Functions includes a perpetual free tier for invocations to allow you to experiment with the platform at no charge. Note that even for free tier usage, we require a valid billing account.


1 Answers

UPDATE: The cold start times seem to no longer be an issue with node 8, at least for me. I'll leave my answer below for any individuals curious about keeping their functions warm with a cron task via App Engine. However, there is also a new cron method available that may keep them warm more easily. See the firebase blog for more details about cron and Firebase.


My cold start times have been ridiculous, to the point where the browser will timeout waiting for a request. (like if it's waiting for a Firestore API to complete).

Example A function that creates a new user account (auth.user().onCreate trigger), then sets up a user profile in firestore.

  • First Start After Deploy: consistently between 30 and 60 seconds, frequently gives me a "connection error" on the first try when cold (this is after waiting several seconds once Firebase CLI says "Deploy Complete!"
  • Cold Start: 10 - 20 seconds
  • When Warm: All of this completes in approximately 400ms.

As you can imagine, not many users will sit around waiting more than a few seconds for an account to be setup. I can't just let this happen in the background either, because it's part of an application process that needs a profile setup to store input data.

My solution was to add "ping" function to all of my API's, and create a cron-like scheduler task to ping each of my functions every minute, using app engine.

Ensure the ping function does something, like access a firestore document, or setup a new user account, and not just respond to the http request.

See this tutorial for app engine scheduling: https://cloud.google.com/appengine/docs/flexible/nodejs/scheduling-jobs-with-cron-yaml

like image 187
Matthew Rideout Avatar answered Oct 27 '22 00:10

Matthew Rideout