Function Scaling and Isolation With Cloud Functions for Firebase (Firecasts)

Firebase · 19K views · 2019 · 4:24

Open on YouTube

Cloud, DevOps & reliabilityWeb developmentexplainer

Why Stack Overflow recommends it

Stack Overflow users, mostly Doug Stevenson himself, link it when explaining that Cloud Functions run in parallel on isolated instances that share no memory, globals or /tmp space.

Recommended by 2 people across 5 different months, 2019 to 2021.

What it is

A short Firecasts episode from the Firebase team in which Doug Stevenson explains how Cloud Functions for Firebase scale across server instances and how each function invocation is isolated from the others. It covers concurrency, separate instances, and what that means for global variables and /tmp.

What people said

Recommends Doug Stevenson · 2019-02 · Stack Overflow · on “Creating Folders in the tmp Directory in Firebase Cloud Functions”

Cloud Functions only allows one function to run at a time in a particular server instances. Functions running in parallel run on different server instances, which have different /tmp spaces. Each function invocation runs in complete isolation from each other. You should always clean up files you write in /tmp so they don't accumulate and cause a server instance to run out of memory over time. You may find it helpful to watch this video about function isolation: [this video] And this video about managing resources, especially /tmp: [LINK]

Recommends Doug Stevenson · 2019-03 · Stack Overflow · on “Firebase cloud functions - what happens with multiple HTTP triggers at once”

Cloud Functions will automatically scale up the server instances running your functions when it determines that more capacity is needed. Those instances will run your function concurrently. The instances will be scaled down when they are no longer needed. The exact behavior is not documented - it should be considered an implementation detail that may change over time. To learn more about this, watch my video about Cloud Functions scaling and isolation [this video] .

Recommends Doug Stevenson · 2019-03 · Stack Overflow · on “Cloud Functions - How to instantiate global functions/variables only once?”

What you're doing isn't a supported use case for Cloud Functions. Cloud Functions code runs in response to events that occur in your project. There is no "one time" function invocations that happen on deployment. If you need to run code a single time, just run that from your desktop or some other server you control. You should also strive to minimize the amount of work that happens in the global scope of your functions. Globals will be instantiated and run once for each allocated server instance running a function in your app, as each function runs in full isolation of each other, and each has its own copy of everything. Watch my video about function scaling and isolation [this video] to better u

Recommends Doug Stevenson · 2019-03 · Stack Overflow · on “Task inside Cloud Functions Authentication trigger needs async?”

Your code, as shown, will not run as expected, because the first return will prevent the second return from ever executing. If you have two items of asynchronous work that don't need to be performed in order, you should just instead collect all the promises into an array and use Promise.all() on that array. Another thing to clarify - Cloud Functions does not deal with the concept of threads in JavaScript. There is just asynchronous work that executes per function, and each function invocation runs in its own server instance, isolated from all other function invocations. You may want to watch my video [this video] that explains this in more detail.

Recommends Doug Stevenson · 2019-04 · Stack Overflow · on “Is it possible to create an atomic transaction covering multiple nodes using Firebase Cloud Functions on Realtime database?”

No, this is not possible. Cloud Functions can't deal with the database differently than any other database client. Cloud Functions can (and often will) run in parallel on different server instances. Watch this video for more information [this video] .

Recommends Doug Stevenson · 2019-04 · Stack Overflow · on “What is the pattern for Google Cloud Functions to implement mutex”

This is not a pattern that you want to implement in Cloud Functions. Restricting the parallelism of Cloud Functions would limit its scalability, which is counter to the way Cloud Functions works. To learn more about how Cloud Functions scales, watch this video [this video] . If you have a database that needs to have some protection against concurrent access, you should be using the database's own transaction features. Pretty much every database that provides concurrent access to data also provides some ability to perform atomic transactions. Use these transactions, and let the serverless container scale up and down in the way it sees fit.

Recommends Harif Velarde · 2019-11 · Stack Overflow · on “Firebase Cloud Function and multiple requests”

I don't think this is related with your code, seems more related how you can manage the Cloud Functions service. Cloud functions will automatically scale up when it determines that more capacity is needed. The instances will be scaled down when they are no longer needed. I recommend you take a look at this document where you can find useful information about how to use Pub/Sub triggers on Cloud Functions, Pub/Sub is a globally distributed message bus that automatically scales as you need it [1]. Also I suggest you check these videos where is explained how works the function scaling [2] and how to deal with parallel execution [3]. [1] [LINK] [2] [this video] [3] [LINK]

Recommends Doug Stevenson · 2020-05 · Stack Overflow · on “How do I share information between functions on Firebase realtime database”

Cloud Functions cannot share information through shared global variables. Each function runs completely independently from each other and have no shared state. You're going to have to find a way to share data by some other means, such as another location in the database. I suggest watching this for more information: [this video]

Recommends Doug Stevenson · 2020-05 · Stack Overflow · on “How do I reduce cold start time using google cloud function instances?”

What you're trying to do is not possible. By definition, two server instances cannot share memory. They are completely isolated from each other. Each function invocation runs isolated in its own instance, and two different functions can never reuse the same instance. You will have to accept that the global memory space for function1 will never be seen by function2. Watch this video to learn more: [this video]

Cites Felix K. · 2021-03 · Stack Overflow · on “Best approach to minimize cold start time in multiple cloud functions”

TL;DR: From a cold-start perspective "Method 2" looks more favorable because you are (A) avoiding to load unneeded modules and (B) avoid to instantiate unnecessary services until they are actually required in your code Long Answer: Mainly two aspects play a role why "Method #2" leads to faster cold starts: The first aspect is that by employing require syntax locally within your handlers instead of using global imports , you effectively avoid loading any unneeded modules. In a project with many handlers depending on entirely different sets of modules and services, this can result a noticable speed improvement. On top of that, it will also optimize the resource usage of your cloud function ser