Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis sorted set leader board ranking on same score

I'm using Redis sorted set to implement the leaderboard of my game, where I show the user ranking in descending order. I'm stuck in a case where two or more users have the same score. So in this case, I want the higher ranking of the user who gets the score first. For example, I'm adding the following entries in Redis.

127.0.0.1:6379> zadd testing-key 5 a
(integer) 1
127.0.0.1:6379> zadd testing-key 4 b
(integer) 1
127.0.0.1:6379> zadd testing-key 5 c
(integer) 1

and when I'm querying for the rank in reverse order, I'm getting this

127.0.0.1:6379> zrevrange testing-key 0 10
1) "c"
2) "a"
3) "b"

but in my case, the ranking should be like

1) "a"
2) "c"
3) "b"

So is there any provision in Redis to give higher precedence to the entity which entered first in the set with the same score?

like image 395
ankit.vishen Avatar asked Oct 05 '18 06:10

ankit.vishen


2 Answers

I found out one solution to this problem. In my case, the score is an integer so I converted it into decimal and added Long.MAX_VALUE - System.nanoTime() after decimal. So the final score code looks like

double finalScore = score.(Long.MAX_VALUE - System.nanoTime());

So the final score of the player who scored first would be higher than the second one. Please let me know if you have any better solution.

like image 64
ankit.vishen Avatar answered Sep 18 '22 05:09

ankit.vishen


If your leaderboard's scores are "small" enough, you may get away with using a combination of the score and the timestamp (e.g. 123.111455234, where 123 is the score). However, since the Sorted Set score is a double floating point, you may lose precision.

Alternatively, keep two Sorted Sets - one with each player's leaderboard score and the other with each player's score timestamp, and use both to determine the order.

Or, use a single sorted set for the leader board, encode the timestamp as part of the member and rely on lexicographical ordering.

like image 34
Itamar Haber Avatar answered Sep 21 '22 05:09

Itamar Haber