Home > OS >  Firestore reads and cache
Firestore reads and cache

Time:05-06

I'm trying to understand more about the reading from the server and reading from the cache and what is billable.

In this answer to a similar question Alex states:

However, if you perform a query, "again and again", as long as it's the same query and nothing has changed on the server, then you will not be charged with any read operations. This is happening because the second time you perform the query, the results are coming from the cache.

How do we know that the second query (identical to the first) is coming from the cache when the snapshot.metadata.isFromCache field is still false? Another from Alex states similar thoughts here.

According to Doug in comments here he states:

The bottom line is this: unless you are offline or querying the cache explicitly, you are charged for all results that come from the server.

These seem to be opposing views. I understand I can explicitly query the cache, that's not in question. I'm just trying to understand if I run the same query 10 times and 10 documents are returned each time (similar to Alex's example/comment) will I be charged for 100 reads or 10? (assuming the network is available the whole time).

Any thoughts? Thanks in advance.

CodePudding user response:

I wrote an article called:

How to drastically reduce the number of reads when no documents are changed in Firestore?

In which I have explained everything regarding Firestore billing. I have also added a workaround on how to reduce the number of reads when there are no documents changed in the database.

And to answer your questions:

However, if you perform a query, "again and again", as long as it's the same query and nothing has changed on the server, then you will not be charged with any read operations.

This is happening as long as the (real-time) listener stays active.

Regarding the second answer, if you are listening for real-time changes and you perform a second query, within the 30 min limit, the results will always come from the cache. However, if you're only using a get() call, then each time you perform such an operation, you'll be billed as in the case of performing a brand new query.

When you are offline, and as long as you have the offline persistence enable, which is by default enabled in Firestore, you'll always read the data from the cache. Cache reads have no cost.

if I run the same query 10 times and 10 documents are returned each time (similar to Alex's example/comment) will I be charged for 100 reads or 10?

If you're using a get() call, you'll be charged with 100 reads, but if you're listening for real-time updates and the listener isn't disconnected for 30 minutes, then you'll have to pay only 10 reads.

  • Related