0

I am developing an app that uses Firebase cloud functions ( Node.js ) , what I want to achieve is to trigger a function when a child is created in node a , and then it reads another node b , loop through data snapshot and choose a single child based on filters :

{
    "a": {
        "id1": {
            "filters": {
                "field1": "value3"
            }
        }
    },
    "b": {
        "child1": {
            "field1": "value1",
            "field2": "value2"
        },
        "child2": {
            "field1": "value3",
            "field2": "value4"
        }
     ...
    }
}

using orderByChild and equalTo is not enough because i have more than one filter and also i have a range filter (age between min and max) , so i have to loop through node "b" to choose the child.

my question is can i store node "b" in a global variable ( array ) instead of reading the entire node b each time ( i will read from the variable instead ) ? if yes is there a size limit to that variable ? and would it be faster than reading from database each time ?

5
  • It appears that the data structure might need a revision, however if you could kindly update your question with exact details on what you wish to achieve (at higher level), upon creation of a new child node under node a, it'd be helpful to review structure of node b and thereby the logic. Commented Sep 22, 2018 at 7:06
  • @Neelavar it is not my data structure it is just a sample to explain what i mean , i think there is no need for details to answer my question , my question simply is : can i store node b in a global variable ( outside cloud function ) instead of accessing it from database each time the function is triggered ? and if it has size limitation ? , would it be faster ? Commented Sep 22, 2018 at 7:52
  • As per firebase official docs (cloud.google.com/functions/quotas) - each function is limited with a memory allocation of 2048MB. Hence, we'll have to ensure that the function doesn't get killed if it exceeds the memory allocation quota. When the function is triggered onWrite of node a, how do you intend to store node b outside the scope of the function? Commented Sep 23, 2018 at 2:03
  • this is my question if i can use a global variable to store node b ? and then access that variable in onWrite of node a Commented Sep 23, 2018 at 11:26
  • How are you planning to instantiate the global variable? Because looking at your example data structure, it appears you'll have to read it from Firebase. So will you read that data to instantiate your global variable, when your cloud function is executed the first time? Commented Sep 24, 2018 at 13:56

1 Answer 1

2

Cloud Functions use concurrency and are stateless.
From documentation -

To allow Google to automatically manage and scale the functions, they must be stateless—one function invocation should not rely on in-memory state set by a previous invocation

However, for optimization purpose, existing state can be often reused.
Here is tip from Documentation regarding using global variables -

There is no guarantee that the state of a Cloud Function will be preserved for future invocations. However, Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.

A global variable will be declared (and can be instantiated) at cold-start.
However, looking at your example data structure, if the data resides in Firebase and it can change/update, there will be additional overhead of keeping your global variable updated.

You may want to re-consider your design.
For accurate results from your function, you may need updated data from node b available for filtering results and reading that from Firebase at the time of invocation may not prove costly vis-à-vis checking and updating your global variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.