I actually have a working leaderboard app which uses redis. You can check it out here. In my app the leader boards are limited by the top n
scores, and also old scores drop off when they are too old (so there can be day, week, month, year high score boards).
Anyways, I think what you are looking to do is an overall leader board, so low scores get pushed off. In the sense of Redis Sorted Set scores, if you have it set up so high scores should stay (they are at the bottom) and low scores should leave (they are at the top) then you would do something like:
ZREMRANGBYRANK leaderboard 0 -100
That example assumes you want to keep the last 100 scores.
Maybe you invert the scores, so a "high score" of 1000 is stored as -1000 in redis, so its first in the sorted set. In that case it would be the same as above, but use ZREMRANGEBYRANK leaderboard 100 -1
to delete all the items after the first 100.
Update: Realized there is ZREMRANGEBYRANK
so simplified my example.
Your proposed solution should work as well. If you want to find the score of the nth
element, you can use this:
hundredth_entry = ZRANGE leaderboard 100 100
hundredth_score = ZSCORE leaderboard hundredth_entry
ZREMRANGEBYSCORE leaderboard -inf (hundredth_score
The (
is there to make the range NOT inclusive, so anything LESS than the hundredth score will be deleted but not the hundredth score itself.