Desired outcome
My app stores the events
and responses
of my users in Firebase Cloud Firestore. A user can save a response with different types regarding his attendance (e.g. "yes", "no", "maybe", "on vacation").
A counter keeps track of the different response types and is stored in the event document. Whenever a response is added or updated, a Cloud Function (onWrite
trigger) will increase or decrease the counter in the event document. When all events are listed, the total number of responses can be shown immediately using the data from the counter fields.
Data structure in Cloud Firestore:
[events] / {eventDoc} / [responses] / {responseDocs}
The eventDoc
stores all of the event details (e.g. title, start, end, location).
When users reply with their personal attendance details (e.g. "yes", "no", ...), a new responseDoc
is created in the responses
collection with their uid. So each response is a separate document!
Problem
Because of the 1 write per second limit, I am concerned that there may be delays and especially errors in the counters if a large number of users update their responses at the same time. The counters make little sense if they do not display correct data.
Should a distributed counter be used in this case?
I'm not sure if a distributed counter is even necessary here because user responses are not stored in one eventDoc, but in separate documents in the sub-collection.
Question
Should I use a triggered Cloud Function as described above or should I implement the adjustment of the counter on the client?
A transaction on the client side could ensure that a response would only be created or updated if the corresponding counter value on the event document is also updated. However, this could just lead to frustrated users, if the execution fails. And what about idempotent Cloud Functions?
Any feedback on this is very much appreciated!