Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - does caching improve performance?

I'm building a web application using Firebase that takes the same data and presents it in two different ways - in a list and as an markers on a google map.

Right now, in each view - map or list - I have code to query data from firebase, merge it together, and display it. Instead, I'm considering this plan: on startup, query the data, merge it, and save it all into an array that I pass from view to view.

In a sense, I am "caching" the firebase data in an array. This isn't ideal in one sense - cached data isn't as up-to-date as directly querying Firebase. On the other hand, I only call Firebase once.

Does this make performance sense? Does reading data from a Firebase take within the same order of magnitude time as reading data from an array?

like image 791
PersianExcursion Avatar asked Aug 16 '12 16:08

PersianExcursion


People also ask

Does firebase use cache?

Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

Why are my firestore reads so high?

Use of the Firebase console will incur reads. If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads. You can go through this answer.


1 Answers

In general, caching data to limit use of Firebase is unnecessary. Firebase maintains its own cache of "active" data on the client. "Active" is defined as data for which an "on" call is outstanding on it. Therefore for any active data, any additional "on" or "once" calls will require no network traffic as the data has already been loaded.

I'm not entirely sure what you mean by "querying firebase". Firebase does not have queries in the traditional sense. It simply has methods for attaching callbacks. Are you using the "once()" function to get data periodically from Firebase? If so this could potentially be very inefficient. Once is a convenience method and should generally only be used for data that is accessed extremely infrequently or that for some reason the developer does not want to update in real-time. If no active "on" calls are outstanding when the once() completes, Firebase will flush the cache of that data, and any subsequent calls to once() will require a roundtrip to the server.

If what you want is a way to synchronously access a local copy of the latest version of the data in an efficient way, I recommend this method:

var savedSnapshot = null;
dataRef.on("value", function(snapshot) {
  savedSnapshot = snapshot;
});

//and then when you need to read the data
var theData = savedSnapshot.val()

By maintaining one single on() call, Firebase is able to keep your data up-to-date by only sending deltas over the wire when things change, rather than reloading all of the data every time you need it.

like image 57
Andrew Lee Avatar answered Nov 03 '22 22:11

Andrew Lee