DMonkey
DMonkey

Reputation: 305

What is the read count for realtime listening of cloud_firestore query and can it be done better?

Imagine I have the following pseudo code in flutter/dart :

  Stream<List<T>> list() {
      Query query = Firestore.instance.collection("items");
      return query.snapshots().map((snapshot) {
        return snapshot.documents.map((doc) {
          return standardSerializers.deserializeWith(serializer, doc.data());
        }).toList();
      });
  }

I am listening to the whole collection of "items" in my database. Let's say for simplicity there are 10 documents in total and I constantly listen for changes.

I listen to this stream somewhere in my code. Let's say this query returns all 10 "items" the first time this is called for example. This counts as 10 reads, ok fine. If I modify one of these documents directly in the firestore web interface (or elsewhere), the listener is fired and I have the impression another 10 reads are counted, even though I only modified one document. I checked in the usage tab of my cloud project and I have this suspicion.

  1. Is this the case that 10 document reads are counted even if just one document is modified for this query?

  2. If the answer is yes, the next question would be "Imagine I wanted to have two calls to list(), one with orderBy "rating", another with orderBy "time" (random attributes), one of these documents changes, this would mean 20 reads for 1 update"?

Either I am missing something or firestore isn't adapted for my use or I should change my architecture or I miscounted.

  1. Is there any way to just retrieve the changed documents? (I can obviously implement a cache, local db, and timestamp system to avoid useless reads if firestore does not do this)

pubspec.yaml =>

firebase_database: ^4.0.0
firebase_auth: ^0.18.0+1
cloud_firestore: ^0.14.0+2

This probably applies to all envs like iOS and Android as it is essentially a more general "firestore" question, but example in flutter/dart as that is what I am using just in case it has something to do with the flutterfire plugin.

Thank you in advance.

Upvotes: 2

Views: 1833

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83191

Q1: Is this the case that 10 document reads are counted even if just one document is modified for this query?

No, as detailed in the documentation:

When you listen to the results of a query [Note: (or a collection or subcollection)], 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.)

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. [Note: So 10 reads in your example.]

Q2: If the answer is yes, the next question...

The answer to Q1 is "no" :-)

Q3: Is there any way to just retrieve the changed documents?

Yes, see this part of the doc, which explains how to catch the actual changes to query results between query snapshots, instead of simply using the entire query snapshot. For Flutter you should use the docChanges property.

Upvotes: 3

Related Questions