How to increment a counter stored in database using Faunadb and GraphQL?

If 1000s of users in your application are writing to the same document at the same time keep in mind that this might create conflicts which could slow down writes.

This is something you shouldn’t worry about prematurely except in case you are already sure that this is going to happen. If that happens there are techniques to work around that, in essence you would split up the count in multiple counts. For example, if the data model allows it you could consider splitting up the counts in multiple documents per user, per group of users, etc. If you don’t have something in your model where it makes sense to split counts into you could just instead of creating one document, create N count documents (e.g. 10 documents) to keep one specific count.

On write, you would randomly pick one of these documents (or the document linked to the user or user group if you took that approach).
On read, you sum the write. So it’s a tradeoff between a less performant read but a more performant write.

As I said, only worry about that when your going to heavily write with many users to one document but I thought it was important to know :slight_smile:

2 Likes