-1

I am building a community that operates on reputation similarly to SO. I need to keep a leading board of the users with the most reputation. Preferably by percentage (for example top 5% of users).

I am using cloud firestore. Currently the best option I can think of, is that when a user log in I make a read query for users, order the results by reputation and limit it to whatever 5% of the current amount of users is, and then from the results I create the leading board.

The problem though is that this could lead to huge amounts of reads as the community grows. If for example the community has 100k members, then every login the user end needs to read 5k documents just to get the leading board.

The other option I can think of is to have one document of the leading board and whenever someone's reputation changes then I edit that document. But this would lead to many writes, as just like SO reputations would be changing quit often.

*its important to mention: I need the leading board just to grant certain permissions to users on it that others don't get. So other than knowing if the user is part of those 5% or not there's nothing else I need from it. There's not an actual leading board page where you can see the users' profiles or anything like that.

Can anyone suggest better ways of approaching this?

Tsabary
  • 3,119
  • 2
  • 24
  • 66
  • 1
    Do you really need to have the top 5% updated in real time? On SO, for example, they update the leader board once a day . . . – Jim Mischel Jul 14 '19 at 00:06

1 Answers1

1

You need to think of what data the user realistically is going to request, and then consider if you want to optimize for that. For example: the chances of many users reading the top 5,000 (5% of 100K) users is quite small, while the chances of them reading the top 50 is a lot higher. So instead of requesting the top 5%, you might be better off only requesting the top 50 users.

Even better: you could add the basic information that you display on the leaderboard for those top 50 users into a single document. That way you'd only have to request a single document to display the leaderboard. You can then either update this "leaderboard" document on every write, or (more likely) periodically as Jim commented.

This way of modeling data to match what you're showing on the user's screen is quite common in NoSQL databases. To learn more about it, I highly recommend reading NoSQL data modeling, watching Getting to know Cloud Firestore, and reading the top answer on Leaderboard ranking with Firebase.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807