0

I am trying to build leaderboard for which I am using redis sorted set. I need to return user rank who is invoking api /getMyRank?uid=55

I need to return users rank using dense ranking method. For those who are unaware of dense ranking here is the example | uid | score| dense rank | --- | -----| ---------- | 11 | 10 | 1 | 22 | 10 | 1 | 33 | 20 | 2 | 44 | 20 | 2 | 55 | 30 | 3

When I use ZRANK testKey 55 command of redis I get value 5 but I need to return to user 3

any suggestions on how to accomplish user rank using dense ranking?

Few options in my mind

  1. bruteforce - fetch all the scores from 0 till my score and apply dense ranking algorithm on it -- this is pretty heavy space and time complex tax when my data will be large

  2. (totalUniqueScoreCount above my score) + 1 -- finalScore = 2 + 1 = 3

but the problem with approach 2 is I need to have total unique score count

For calculating this I need to use redis hash with score as key and value as count and when calculating the user score I need to traverse the hash to get the unique scores whose count > 0. This operation is computational intensive plus it can give data inconsistency in case of race condition.

anonymous
  • 47
  • 10

0 Answers0