4
votes
  • I am using the firebase JS SDK, with offline persistence enabled. This will automatically cache my retrieved documents inside the JS SDK.
  • I perform this query:
// once
firebase.firestore().doc('path/to/doc').get()
// twice
firebase.firestore().doc('path/to/doc').get()

My questions are:

  1. Would Firebase charge me for two reads in the case offline persistence is enabled. Since I believe the second read would come from the offline cache without making a network call?
  2. Would Firebase charge me once or twice when offline persistence is disabled?

My use case

A user opens a web-app with his 'tasks' from clients.
Each task has a client field with a document reference.

  1. retrieve all tasks (eg. 500 reads)
  2. retrieve each client document per task (another 500 reads even though some clients are the same)

So if there are only say 20 clients, I'd rather have only 520 reads instead of 1000 reads when the user opens the app.

Is it better to redesign my data structure to have the relevant client information saved on the task itself instead of a reference, or will the Firebase JS SDK be smart enough to not double retrieve the same doc it just retrieved.


PS: I found a similar question here:

Does Firestore charge Cloud Function Get reads per retrieved documents?

But it just didn't have the exact information I was looking for.

1

1 Answers

6
votes

does firebase charge for retrieving docs from firestore js sdk cache?

No it doesn't. If a read is fulfilled from the local cache, it is not charged as a server-side read.

But your code has two get calls:

// once
firebase.firestore().doc('path/to/doc').get()
// twice
firebase.firestore().doc('path/to/doc').get()

And in this case, the Firestore client has no way to know whether the document was changed between these two calls. So it has to call to the server to determine whether the document was changed, which is (or at least may be) a charged read.

If you need to know the current state of a document in multiple places in your app that execute close to each other (time wise), you should keep an onSnapshot listener on that document. This ensures that the local cache is always up to date at a minimal cost.