Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize firestore read per app launch

Tags:

Understand firestore charge based on read / write operation. But I notice that the firestore read from server per app launch, it will cause a big read count if many user open the app quite frequent.

Q1 Can I just limit user read from server for first time login. After that it just read for those update document per app launch?

For example there's a chat app group. 100 users 100 message 100 app launch / user / day

It will become 1,000,000 read count per day? Which is ridiculous high.

Q2 Read is count per document, doesn't matter is root collection / sub collection, right? For example, I read from a root collection that contain 10 subcollection and each of them having 10 documents, which will result 100 read count, am i right?

Thanks.

like image 207
Dodo Avatar asked Aug 22 '19 05:08

Dodo


People also ask

How do you reduce the number of read operations in cloud firestore?

This means that Cloud Firestore will try to retrieve an up-to-date (server-retrieved) snapshot, but fall back to returning cached data if the server can't be reached. This is how we can reduce the number of reads in Cloud Firestore, by forcing the SDK to use only the offline cache and get the data only upon request.

How many reads can firestore handle?

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

That’s correct, Cloud Firestore cares less about the amount of downloaded data and more about the number of performed operations.

As Cloud Firestore’s pricing depends on the number of reads, writes, and deletes that you perform, it means that if you had 100 users communicating within one chat room, each of the users would get an update once someone sends a message in that chat, therefore, increasing the number of read operations.

Since the number of read operations would be very much affected by the number of people in the same chatroom, Cloud Firestore suits best (price-wise) for a person-to-person chat app. However, you could structure your app to have more chat rooms in order to decrease the volume of reads. Here you can see how to store different chat rooms, while the following link will guide you to the best practices on how to optimize your Cloud Firestore realtime updates.

Please keep in mind that Cloud Firestore itself does not have any rate limiting by default. However, Google Cloud Platform, has configurable billing alerts that apply to your entire project.

You can also limit the billing to $25/month by using the Flame plan, and if there is anything unclear in your bill, you can always contact Firebase support for help.

Regarding your second question, a read occurs any time a client gets data from a document. Remember, only the documents that are retrieved are counted - Cloud Firestore does searching through indexes, not the documents themselves.

By using subcollections, you can still retrieve data from a single document, which will count only as 1 read, or you can use a collection group query that will retrieve all the documents within the subcollection, counting into multiple reads depending on the amount of documents (in the example you put, it would be 10x10 = 100).

like image 59
Deniss T. Avatar answered Oct 15 '22 09:10

Deniss T.