Minimizing cold start time (Firecasts)

Firebase · 34K views · 2019 · 5:53

Open on YouTube

Cloud, DevOps & reliabilitySoftware performance & optimizationWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about slow first invocations of Firebase Cloud Functions and how to reduce cold start time through lazy loading of dependencies.

Recommended by 9 people across 9 different months, 2019 to 2023.

What it is

A short Firebase 'Firecasts' episode, the last in a series on Cloud Functions, presented by Doug Stevenson. It explains how cold starts happen and how to minimize them, mainly by moving global-scope code and imports into the specific functions that need them.

What people said

Strong rec HungryArthur · 2022-02 · Stack Overflow · on “The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError”

There is a performance argument to have the admin initializeApp within the function and not global scope. You keep a global boolean var that you can use to decide whether your specific function needs to initialize. Here's a great YouTube video on this topic by Firebase that's well worth a watch: [this video] Example: GLOBAL: let isSpecificFunctionAdminInitialised = false; WITHIN FUNCTION: const admin = require("firebase-admin"); if (!isSpecificFunctionAdminInitialised) { admin.initializeApp(functions.config().firebase); isSpecificFunctionAdminInitialised = true; }

Recommends Sebastian Vischer · 2019-04 · Stack Overflow · on “Slow Firebase Firestore cold starts in Cloud Functions”

If the cloud function needs to start a new instance your cold start time seems normal. This is one drawback of a serverless function. I think there is a problem with your implementation. Could you show more details? Here is a nice little video about this topic: [this video]

Recommends ajorquera · 2019-11 · Stack Overflow · on “Cloud Functions (Firebase) - decrease cold start times through partial loading firebase modules?”

There are a few strategies to decrease cold starts for firebase functions. Your strategy is on spot to try to reduce the dependencies. 1 contributor to GCF cold-boot performance is dependencies. It doesn't seem possible the kind of granularity you want. My suggestions are: Try to use the version of the libraries that is more widely used. The dependency cache is shared across all GCF dependencies. There is a very good post about this subject from a guy from google. Check it out [LINK] . Also, there is the video version [LINK] Use lazy loading for dependencies. There is a nice video [this video] explaining this strategy by @DougStevenson, a Firebase member. He also gives an example for using typesc

Recommends Doug Stevenson · 2020-02 · Stack Overflow · on “Single Cloud Function to call 100+ local functions”

It's not possible to entirely eliminate cold start times. They will always exist in serverless environments - that's a matter of fact. The best you can do is organize your code to minimize the cold start times. Without seeing all your code and your specific benchmarks, there is no specific advice we can geive. The only thing that's in your control is to make sure that each function only loads the minimum amount of code at the global scope to run correctly. So, if your functions all load some code at the global scope that's unused, all those functions will pay an unnecessary cold start cost to load and run the code they don't use. There are resources out there that will help you do that. Watc

Recommends David · 2020-05 · Stack Overflow · on “Trying to understand / improve cause of cloud function cold start”

Because Cloud Functions are stateless, the execution environment is often initialized from scratch, so you will see cold starts. It may be helpful to review Tips & Tricks For Cloud Functions [LINK] . Also, you may want to check out this informative video made by the Firebase team about Minimizing Cold Start Time [this video] which basically explains that minimizing the amount of work your function performs at the global scope for all the code that imports, both directly and indirectly, is the key to minimizing cold starts. You can also try periodically pinging the function to minimize cold start time. This Page [LINK] may be helpful for that, but it’s a bit outdated, so you may need to do some fu

Recommends ThienLD · 2020-08 · Stack Overflow · on “Why does Firebase take forever to complete the same function after redeploying an app?”

Its seems like you are experiencing cold start with Cloud Functions. As far as I'm aware, you can't avoid it since your functions need to be initialized the first time they are called after redeploy. This [this video] is a video about cold start and how to minimize its impact on your app.

Recommends Daniel Ocando · 2020-09 · Stack Overflow · on “Understanding Firebase cloud function "cold start"”

You've reached the correct section of the documentation. I think you'd find the following video [this video] very useful as well!

Recommends Marius Wilhelmi · 2020-11 · Stack Overflow · on “Initial response from Dialogflow fulfillment library takes very long (30 sec)”

Could be related to something called "cold start". Will try to try out the suggestions given in this video, but it will take some time until I can report back: [this video]

Recommends Monali Ghotekar · 2022-08 · Stack Overflow · on “Is there a way to warm up a Firebase cloud function before it is called so it is faster?”

As @John Hanley mentioned you can call the function asynchronously. That will cold start an instance. You can checkout the recommendations mentioned in Document [LINK] to optimize your deployed function which will reduce the cold start time. Please checkout the video minimize cold start time [this video] for details. You can refer to the document for another workaround Cold start workaround [LINK] . To avoid the cold start you may want to consider running your own server instances using something like App Engine to have warm up requests [LINK] .

Cites Doug Stevenson · 2019-06 · Stack Overflow · on “How to properly do tree shaking to reduce bundle size and separate entry point for each cloud function”

You can create a different local Firebase working area (with firebase init ) for each function that should deploy in isolation from the others. You will have to instruct the CLI not to overwrite the other functions on deployment using the --only functions:yourFunctionName to deploy it. Or, you can deploy function using Cloud tools (gcloud) instead of Firebase tools, but you won't be able to use firebase-functions and its TypeScript bindings. Or, you can lazily load your modules instead of statically loading them at the global scope of your functions, as described in this video [this video] . I don't recommend using webpack. It's not going to be worth your time to get it configured.