Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about firestore cache and reads charge

There are my understand how firestore cache and read charge will behave in those scenarios.And I am not sure it's right or not.

If I have 2000 documents in a collection.this collection name "testCollection".those documents won't change , update or delete. My client is android with already enable offline persistence and device is currently online

1.

db.collection("testCollection").onSnapshot(function(doc) {});

2000 reads charged and those document are cached. then reopen app and run same code again

db.collection("testCollection").onSnapshot(function(doc) {});

another 2000 reads charged cause firestore need to check each document is up to date or not. So 2000+2000 reads charged

2.

I am not sure how this behave. just run the same code together

db.collection("testCollection").onSnapshot(function(doc) {}); 
db.collection("testCollection").onSnapshot(function(doc) {});

I think is 2000 reads charged because data is keeping up to date

3.

db.collection("testCollection").limit(300).onSnapshot(function(doc) {}); 
db.collection("testCollection").limit(800).onSnapshot(function(doc) {}); 

Total 1100 reads charged.cause it's different query.

Do I have something misunderstand or something wrong ?

like image 802
flutroid Avatar asked Jun 16 '19 10:06

flutroid


People also ask

What counts as a read in firestore?

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

How does firebase cache work?

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.

Does firebase firestore cache?

Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data.

How many documents can a collection hold firestore?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.


1 Answers

2000 reads charged and those documents are cached.

That's correct since it's the first time when you perform those read operations.

another 2000 reads charged cause Firestore needs to check each document is up to date or not. So 2000+2000 reads charged

Once the documents are in your cache and those documents aren't changed (as you say), all read operations are coming from the cache. You won't be charged for read operations that are coming from the cache.

I think is 2000 reads charged because data is keeping up to date

You'll be charged with other read operations only if the data on the Firebase servers is changed, otherwise, you'll get the data from the cache.

Total 1100 reads charged.cause it's a different query

If you have already performed the initial query and you already got those 2000 documents, if you perform another query no matter if you are using a limit or not, you read all those documents from the cache.

like image 103
Alex Mamo Avatar answered Sep 28 '22 07:09

Alex Mamo