Skip to content
Advertisement

What is the best way to reduce firestore document reads in chat app on page load?

I have a chat app that is charging me a large number of reads for each page load, 1 for each message to show. I’m trying to figure out a way to reduce that number and optimize for cost, as refreshing the page a few times costs hundreds of reads.

The firestore pricing documentation says

For queries other than document reads, such as a request for a list of collection IDs, you are billed for one document read. If fetching the complete set of results requires more than one request (for example, if you are using pagination), you are billed once per request.

I considered that maybe if I fetch an entire collection without a query like shown here in the docs, a cost difference might be remotely possible. I’m sure that’s probably wrong, but I can’t find anything specifying what the exceptions are that only cost 1 read. It also crossed my mind to create an array to hold the most recent messages in the parent document of the collection, but the security rules for updating that array seem overly complex and not practical. I also read about using the firebase cache, but that doesn’t seem useful here.

Here is code to demonstrate how I’m currently loading messages. I’m using the react-firebase-hooks library to snapshot this data with useCollectionData:

const q = query(messagesRef, orderBy("createdAt", "desc"), limit(100))
const [messages] = useCollectionData(q)

In researching, I found this question where I’m pretty sure the accepted answer is wrong. It did make me question the rules. Are there any strategies to reduce the number of reads for this common use case?

Advertisement

Answer

Pagination still incurs charges on a per-document read, right?

Yes, it does, but only when you load more pages.

I’m not trying to load the entire collection, but rather wondering if loading the collection without a query has a different cost than with.

Loading a collection without a query that is limiting the results, means that you’re reading the entire collection. And, yes, the cost will be much higher if you’re not using a query. Remember, that the cost of reading a collection/query in Firestore is equal to the number of documents that are actually returned. For example, if you have a collection of 1 million documents, and your query returns 100, you’ll have to pay only 100 document reads.

I’m overall trying to figure out if there’s a strategy that can improve the read cost of the example query I gave.

No. If you need to get the newest 100 messages, that’s the best query you can have. The only change you can make to decrease the number of reads would be to change the value that you pass to the limit() function. And maybe it makes sense since a user might not be interested in reading 100 messages at once. Always try to display data that fits into a screen, and load any other data progressively.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement