arangodb.com/2015/04/creating-multi-game-highscore-lists

I just came across a question about how to create highscore lists or leaderboards in ArangoDB, and how they would work when compared to Redis sorted sets. This blog post tries to give an answer on the topic and also detailed instructions and queries for setting up highscore lists with ArangoDB. The additional section “Extensions” explains slightly more advanced highscore list use cases like multi-game highscore lists, joining data and maintaining a “last updated” date. A highscore list in Redis Highscore lists are normally used to quickly determine who’s currently at the top, so we obviously need some sorted data structure. Redis has a specialized datatype named sorted set which can be used for exactly this purpose. A sorted set in Redis is a value consisting of multiple key/value pairs, and that is automatically sorted by values. The sorted set is stored under a key so it can be accessed as a whole. Here’s how one would create a sorted set named highscores and populate it with 5 key/value pairs in Redis (using redis-cli): [crayon-553641c91b136983911935/] Adding a new entry to a sorted set is done using ZADD too. Inserting into a Redis sorted set has logarithmic complexity. Updating a score in the sorted set is done using ZINCRBY. This command works regardless of whether the to-be-updated key already exists in the sorted set. If it exists, its score will be increased by the specified value, and if it does not exist, it will be created with the specified value: [crayon-553641c91b14b522273231/] In this case the return value 1 indicates that a new key was added to the set and that it didn’t update an existing one. Querying the entries with the lowest scores from a Redis sorted set is trivial. The ZRANGE command will query the entries in the sorted set from lowest to highest score. As the entries are already stored in sorted order, this is very efficient. The following command queries the bottom 3 keys from the sorted set: [crayon-553641c91b157307386517/] For querying in reverse order, there is ZREVRANGE. Both commands can be accompanied by the WITHSCORES flag to also return the associated values (i.e. the scores). Here are the top 3 key/value pairs in the sorted set: [crayon-553641c91b161700266950/] For removing an entry from a sorted set there is ZREM: [crayon-553641c91b16b870986812/] There are many more specialized Redis commands for working with sorted sets. The Redis commands prefixed with a Zaaa


Comments (0)

Sign in to post comments.