The Blog
Creating high score tables (leaderboards) using Redis
To my other colleagues at Agora Games, I have one word: FIRST!
Now that I’ve got that out of my system, down to the business at hand. I’ve read a number of articles on Redis that mention how it could be used for high score tables (leaderboards), but I didn’t see any examples that would walk you through exactly how to do that. Time to rectify that since it’s 2011 and we still don’t have flying cars … so ,,|, future.
UPDATE: All of this has been packaged up in the leaderboard gem on GitHub.
Leaderboards are a small, but important part of the engineering we do at Agora Games. As such, when not riding the short bus, I’m looking for ways to improve leaderboard generation for various situations. So, here’s how I might approach creating leaderboards using Redis.
I offer the following gist. Here’s the jist of the gist:
* The following was prototyped using the redis CLI. You can obviously port the logic to your favorite language binding for Redis, *cough* Ruby *cough*.
* You will be using the Sorted Set commands in Redis.
* You will need to be running at least Redis 2.1.6 to use the ZREVRANGEBYSCORE method.
* Add players to the HIGHSCORES “table” using the ZADD method.
* Print out the players (with a given page size) from the HIGHSCORES “table” using the ZREVRANGEBYSCORE method to show scores from highest to lowest.
* There is some bookkeeping code that you would do at the application level in terms of tracking what page you are on, dividing the total # of leaderboard entries into pages for a given page size, etc.
* Create an “Around Me” leaderboard with scores of individuals above and below an individual player using a combination of the ZREVRANK and ZREVRANGEBYSCORE methods.
* Again, there is some bookkeeping code that you would do at the application level in terms of tracking how many players above or below to show and taking into account offsets.
* The data that you store for a given player could be richer than just the player name or player ID. This is probably a larger design consideration if you are considering the “Around Me” leaderboard situation.
UPDATE: 1/3/2011: I updated the gist to do the “Around Me” leaderboard in 2 calls.
You can find more hilarity over on my Twitter account, CzarneckiD.







Comments
Hi!
Is there a reason for using ZRANGEBYSCORE instead of ZRANGE since you’re using +/- inf ? We use a similar approach, where redis keeps rankend leaderboards columns scores in sorted set and helps fetching the actual LB subset from mysql;
Maybe ZRANGEBYSCORE solves the problem of offset boxing for smaller LB and/or cases where you are close to the top and bottom of the LB?
I’ll have my slides online soon, thanks for the post!
E/
I guess there isn’t a reason other than wanting to limit the scores returned if you are using ZRANGEBYSCORE. But, with leaderboards, you’re right, typically you want all the scores regardless.
[...] Please use the original post for any comments/feedback. This post is a [...]
[...] generation for various situations. This effort was really the logical next step to my blog post, Creating high score tables (leaderboards) using Redis. All of the information you’d probably be interested in as a developer in terms of working [...]
Can I get a score table in flash?