How to keep a large unique count?


I'd like to keep a running count of user ID's (throughout a number of documents) and I don't know the best way to approach it given Firestore's 1 MiB document limit. Ideally, I'd like to store each unique user ID in a single array and get the count that way. This makes the task idempotent as (1) each user can only add and remove their own ID's from the array and (2) getting an accurate count is as easy as getting the count of the array. However, the array's size is constrained by the size limit of the document and documents are meant to be small.

The other option is to keep a counter that each user can add to or subtract from. If the user wanted to add themselves to the collection, the client would first get the current value of the counter property and add to it. While this solves the file-size problem, the task is not idempotent. The time it takes the client to retrieve the current value of the property and set it is enough time for another user to do the same thing with a faster connection and with enough simultaneous users this doesn't seem very reliable.

What is the best third option?

- - Source

Answers

answered 4 day ago Doug Stevenson #1

If you have multiple clients writing to the same document, you should use a transaction to make sure the writes are consistent and don't interfere with each other.

comments powered by Disqus