8

I am trying to optimize the number of reads my app makes on Firesbase and reviewing the way I use snapshot to monitor real time changes. Imagine I have a snapshot returning the 10 latest documents like the following:

db.collection("cities")
.where("state", "==", "CA").
.orderBy('dateadded', 'desc').limit(10)
.onSnapshot(function(snapshot) {
    ...
})

The doc says

The snapshot handler will receive a new query snapshot every time the query results change (that is, when a document is added, removed, or modified).

Does that mean that every time the query changes I will be billed 10 reads? In this example if a new city is added (so with the latest 'dateadded', so coming first in the query) will it be 1 (just the new city) or 10 reads (the full query).

As a side question where I can see the exact number of read I am at (live so I can test a function and see the corresponding number of reads). The IAM and Admin/Quota seems to provide that under "reads request per day" but it is not live...

I am referring to the Firestore snapshot as described at https://firebase.google.com/docs/firestore/query-data/listen

1 Answer 1

11

If 1 document changes in query results, you will be billed 1 read. You are not billed for documents that are unchanged. The query snapshot object merely reuses the document data that was in memory from the prior set of results.

8
  • Ok. But note that in the example I give, unchanged documents actually change position in the query result (1st becomes 2nd, 2nd becaomes 3rd and so on) - is your answer still the same? Sill 1 read?
    – l1b3rty
    Commented Feb 5, 2020 at 13:15
  • And any clue regarding my question on following live my consumption?
    – l1b3rty
    Commented Feb 5, 2020 at 13:15
  • 1
    1 query 1 read??? That makes no sense to me... If my query reads my entire collection with !M+ doc, that cant be 1 read!
    – l1b3rty
    Commented Feb 5, 2020 at 21:39
  • 1
    You will initially be billed 10, then 1 for each document changed in the results after that, for as long as the listener stays active. Commented Feb 5, 2020 at 23:33
  • 2
    This is only partially true, you are not billed for documents that are unchanged as long as client stays connected. From official docs: Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.. So be careful Source: firebase.google.com/docs/firestore/pricing#operations
    – tonjo
    Commented Feb 6, 2020 at 9:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.