Leaderboard Rank

Hi :slightly_smiling_face:

I want to display a leaderboard in our mobile app. Each user has a “steps” property. The leaderboard will show the top 10 users with most steps.

The question is, how can display the current user position (rank) in the leaderboard? So for example if the current logged in user made 2500 steps, he is in 7483 place. How can I query efficiently to get that answer?

And also, we need to filter the leaderboard showing only certain segment of users. For exmaple display the leaderboard for all the users in segment 16.

Just for reference in Redis you can use ZRANK. In Mongo we can use the aggregation pipeline with $unwind. In Postgres we can use RANK OVER.

Hey @yaron,

We do not have something like Redis’s ZRank at this point.
I haven’t worked extensively with Mongo but to me unwind and the aggregation pipeline seems similar as doing a Count() on the result of Range(Match(…), 0, 2500) (Range is inclusive).

Of course, you’ll have to take into account values of ‘steps’ that are equal.

Does that make sense?

@databrecht I know devs are using unwind in Mongo for the leaderboard use case, but the question is really how performant is it. Using Redis is very performant but lacks the option to also filter (where) to display different leaderboards.

Regarding the ‘equal steps’ I don’t think it’s such a problem because you’ll just pick the first one for exmaple.

I’m not 100% certain, I’ll ask internally but I think Count is O(n) in complexity while Range will use the index so the performance depends on the amount that needs to be counted.

edit: confirmed :wink:

1 Like