10

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 ?

flutroid
  • 1,166
  • 8
  • 24

1 Answers1

11

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    this is very helpful thanks a lot.I thought that "check document is up to date or not" this action will charge reads. So If those cached documents are same as servers data then I won't get charge right? like if my device cache is empty and first time run `orderBy("timestamp").limit(300).onSnapshot(function(doc) {})` then run `orderBy( "timestamp").limit(800).onSnapshot(function(doc) {})` it should be charged by 800 reads – flutroid Jun 17 '19 at 12:17
  • I am ask this question because https://youtu.be/poqTHxtDXwU this official video in 7:55 say it will charge when you increase limit. I think I am wrong it only happen when you using .get – flutroid Jun 19 '19 at 04:06
  • 1
    If you increase the limit, you'll be charged for the new read operations not for the documents that are already in cache. – Alex Mamo Jun 19 '19 at 06:05
  • @AlexMamo this behaviour is the same for the Firestore web client / regardless offline support, right? – Thijs Koerselman Sep 26 '19 at 10:15
  • @ThijsKoerselman Theoretically yes but keep in mind that for the web, offline persistence is disabled by default. – Alex Mamo Sep 29 '19 at 10:45
  • @AlexMamo I didn't know offline support was even available for web :) But just to be clear; the caching mechanism is always present in the client, also when offline persistence is not enabled, right? – Thijs Koerselman Sep 30 '19 at 17:56
  • @ThijsKoerselman Yes, it is also available for web. No, the caching mechanism is **not** present in the client if you do not explicitly enable offline persistence. – Alex Mamo Oct 01 '19 at 07:21
  • @AlexMamo Thanks for clearing that up. I will have to do some more testing on my code then. It appears that on new collection snapshots I do sometimes get incomplete lists of documents on the first snapshot and the rest later. I'm trying to fully understand the mechanism because I'm developing a library to encapsulate the Firestore documents in observables. – Thijs Koerselman Oct 02 '19 at 08:10
  • @ThijsKoerselman I udnerstand that. Good luck with your tests ;) – Alex Mamo Oct 02 '19 at 08:11
  • Thanks @AlexMamo I don't if I should ask this in a comment or if is better in a single thread but, how does firebase know that the document hasn't changed, doesn't it needed to be checked (i.e count one read) in order to know that the document is still the same? – Fabian Merchan Apr 06 '20 at 04:14
  • 1
    @FabianMerchan There is no need to check something because the listener is triggered only when something changes in the database. – Alex Mamo Apr 06 '20 at 07:53
  • Revisting this thread, if you watch the video if explicitly said you WILL BE CHARGED for the old reads. At the 8:14 mark https://www.youtube.com/watch?v=poqTHxtDXwU has anyone else been able to confirm or is the video from firebase themselves just wrong? – erotsppa Mar 12 '23 at 23:04