Reputation: 133
I have 2 Firestore collections I am worried about. Named Posts and Chats.
POSTS
The setup -- In Posts, each document has a field named Comments (an array of objects). Every time a comment is made on a Post document, the comment obj is added to Comments array, and every time a comment is updated, the comments array is overridden with new set of comments that include the updated comment object.
The issue -- Firestore docs warn against updating documents more than once per second to avoid contention and other errors (which I have witnessed in the Chats collection). How do I avoid this problem? As post comments are expected to update very frequently.
CHATS
The setup -- In Chats, each document has a field named Mesages (array of objects) and each object has a property named received. Messages field works very similar to Comments field. Also in each document is a field named UsersTyping (array of userId strings) which updates every time a users text input changes from empty to not empty and vice versa.
The Issue -- Just like Posts, updates to a document should not happen more than once per second. But because of app requirements, updates to userTyping and message.received need to take place, meaning a lot of updates to the document could happen within a second. How do I avoid this problem?
Tech Stack == React Native, Expo managed workflow, Firebase,
Thank you in advance for your suggestions. 💙
Upvotes: 3
Views: 212
Reputation: 317487
In general, if you have an array of data in a Firestore document, and that array could grow quickly or become very large, it shouldn't be an array at all. Each array element should be its own document, perhaps in a subcollection nested under the primary document. You can add documents to a collection very quickly, and there are no limits to how many documents you can add, so you will not have scaling problems that you're worried about here.
I understand that this costs more document reads and writes. However, this is the way that Firestore is able to scale massively, and you don't really have any easy workarounds to this.
Upvotes: 3