0

How does one cache database data in firebase function?

Based on this SO answer, firebase caches data for as long as there is an active listener.

Considering the following example:

exports.myTrigger = functions.database.ref("some/data/path").onWrite((data, context) => {
    var dbRootRef = data.after.ref.root;
    dbRootRef.child("another/data/path").on("value", function(){});
    return dbRootRef.child("another/data/path").once("value").then(function(snap){/*process data*/})
}

This will cache the data but the question is - is this valid approach for server side? Should I call .off() at some point in time so it doesn't produce any issues since this call can scale quickly producing tons of '.on()' listeners? Or is it ok to keep 'on()' indefinitely?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
vir us
  • 9,920
  • 6
  • 57
  • 66
  • 2
    This sounds like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to accomplish by keeping the data in memory? – Frank van Puffelen Jul 28 '18 at 14:13
  • Thanks @FrankvanPuffelen - seems like you are right about XY problem. My intention is to reduce database downloads that are happening on server side, ie firebase functions. So the function only downloads the required data if it's changed - client can perform multiple writes at "some/data/path" but I want to minimize a number of reads at "another/data/path" – vir us Jul 28 '18 at 18:17

1 Answers1

0

Since active data is kept in memory, your code will keep a snapshot of the latest data at another/data/path in memory as long as the listener is active. Since you never call off in your code, that will be as long as the container that runs the function is active, not just for the duration that this function is active.

Even if you have other Cloud Functions in that container, and those other functions don't need this data, it'll still be using memory.

If that is the behavior you want, then it's a valid approach. I'd just recommend doing a cost/benefit analysis, because I expect this may lead to hard-to-understand behavior at some point.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank - yes I want the data to persist across multiple calls of the same function to reduce number of reads at "another/data/path". How can I calculate the cost of memory for that? Also wondering what are the potential problem with this that can lead to "hard-to-understand behavior"? My understanding is that there will always be actual data in cache which I'll request as needed - just wondering what I'm missing here. – vir us Jul 28 '18 at 18:25
  • One more quick question - is it ok to call "on" multiple times on the same data path or there are any inefficiencies connected with attaching multiple listeners? – vir us Jul 28 '18 at 18:28
  • If there is an active listener on a path, the data won't be redownloaded. – Frank van Puffelen Jul 28 '18 at 22:22