Home > Enterprise >  How to handle Firestore write limits and index exemptions for subcollections?
How to handle Firestore write limits and index exemptions for subcollections?

Time:08-12

I have the following schema for my Firestore database:

[collection]
<document>

[chats]
    <user123>
        [userChats]
            <user789>
            <user345>
            <user567>
    <user789>
        [userChats]
            <user123>
            <user345>
            <user567>

In the userChats subcollection, the documents contain a sequentially-incremented field that is indexed. A sequentially-incremented indexed field imposes a write limit on that collection. Is this limit applied to each subcollection individually? Or is it imposed on all of the userChats subcollections as if they were one since they share the same name?

Furthermore, assume the documents in the userChats subcollection have a private field that I wish to add as a single-field index exemption. What scope would I need to add this exemption, collection or collection-group? And if I choose collection-group, would it have any impact on the write limit mentioned above?

CodePudding user response:

The database doesn't so much impose a limit, as that your choice to use sequential values leads to a hotspot when updating the index for that field. The database simply physically can only write values close to each other (while meeting the cross-datacenter requirements and consistency guarantees of Firestore) at a certain speed, which is why it's best to avoid sequences when possible.

This also shows us the answer to your question: if the values are in separate indexes, as they are for regular indexes, then they won't affect each other's write throughput. On the other hand, if you have an index for a collection group query then it will have to updated for writes to each collection of that name and the throughput is affected for all those writes.

  • Related