firebasegoogle-cloud-firestore

Modeling a game leaderboard with Firestore - does this count as a monotonically increasing field?


I have a video game model where I have a User collection in Firestore where each document has a score field. I want to query to get all users sorted by their score, from highest score to lowest. To do this, I need to index the score field and then I can query all users sorted by their score.

The issue is that the documentation says to avoid creating documents with a monotonically increasing field, like a timestamp, at a very high rate. Does this limitation apply to my use case? Just like timestamps, the score fields are always increasing across documents. But unlike timestamps, the score fields increase at a staggered rate.

For example, one user may have their score go from 1 to 2 to 3 in a one minute period, while another users score may go from 4 to 5 to 6 in that same period of time. Some users play the game often so they may have much larger scores that update more frequently than users who play less often.

Does the limitation to writing/updating documents with monotonically increasing fields apply to my use case?


Solution

  • Does the limitation to writing/updating documents with monotonically increasing fields apply to my use case?

    Yes, it applies. It has to do with the rate of additions within a particular range of values that must be managed by a partitioned index. If you write heavily within a partitioned range, that partition will get overloaded and have to be re-partitioned repeatedly to handle the load. The current size of the range of the partition doesn't matter.