One of the use cases for Redis is creating leaderboards.  Here are some Redis commands that I used in constructing a leaderboard data project.

This document assumes that you are a developer working with Redis for the first time and that you are used to working with SQL data sources.  The best source of documentation available is the Redis homepage (http://redis.io/) which lists and documents every aspect of every redis command available (http://redis.io/commands).  You don’t even really have to bookmark this page because doing a google search on virtually any redis-related topic will point you somewhere in this site.  Redis commands are pretty easy to pick up so this document is going to list out some frequently used operations as they relate to the StudioCycling project.

Setup

If you’re a .NET developer who is comfortable with Windows, the good news is that Microsoft has a port of Redis for windows.  There are two ways to work with it.  The first is to download an installer for a recent build (https://github.com/rgl/redis/downloads) and the second is to get the source of the most recent build and build from that (https://github.com/MSOpenTech/redis).  I used an old version.  It can still connect to our Redis instance but there are a couple of commands that are only in the newest versions of redis that the installer-version doesn’t have.  Fortunately, we don’t use any of them so you can use the installer version with all the code and examples from this project.

It’s probably not a bad idea to put the folder with “redis-cli.exe” into your path so you can use it from the command line.

Frequently Used Redis Commands

List keys (applies to all data)

KEYS <pattern>

list all keys: KEYS *

list all leaderboard keys:  KEYS lb*

NOTE:  Don’t run this in production.  KEYS is a blocking operation so redis can’t do anything else while executing this command

Get a Key (applies to Timeseries data)

GET <key>

so to get a key named “ts.1234567_55555.energy” use:  GET ts.1234567_55555.energy

Get a Hash (applies to Member data)

HGETALL <key>

so to get a key named “member.55555”, use:  HGETALL member.55555

if for any reason you only want to get one field, for instance age, use:  HGET member.55555 age

There is a “multi-get” command also that gets multiple fields, but our data sets are so small in terms of the number of fields that HGET and HGETALL should cover almost all of your needs

See Data in a sorted set (applies to Demographic and Leaderboard data)

ZREVRANGE <key> <start> <end> <options if any>

The <key> we know already.  <start> is the index of the first value to get and <end> is the index of the last.  Note that redis accepts negative numbers and interprets them as offsets from the end of the set counting backwards (so 0 is the first element, 1 is the second element, -1 is the last element, -2 is the 2nd last element).  The only option we use regularly is “withscores” which returns the score as well as the member.

So to get the full leaderboard for club 113 by distance for Oct 2014 use:

ZREVRANGE lb.113.distance.2014.10 0 -1 withscores

Note that ZRANGE is the same thing but sorted in the other direction.  We use ZREVRANGE for leaderboards because we want the highest score to be rank 1.

Filter a Leaderboard

ZINTERSTORE <output set name> <numkeys> <key1> <key2> … <key n> WEIGHTS <weight 1> <weight 2> … <weight n>

This one is pretty complicated and it’s only worth doing a couple of times to convince yourself that it works as expected.  The best way to handle these are to step through the .NET code and see how it works.

Here, <output set name> is the name of a brand-new redis zset that will be created to store the result of the intersection

<numkeys> is pretty much what it sounds like, the number of keys that you are going to intersect

<key 1> … <key n> are the names of all the keys

<weight 1> … <weight n> is a list of weightings to be applied to each set as the intersection is done.  Generally we set the leaderboard weight to 1 and all the rest to 0, which means that the final scores of the intersection set is equal to the scores from the leaderboard data set for each member.  Play around with this and see for yourself what it does if you have the time.

So to filter the leaderboard for club 113 by distance for Oct 2014 to show only male riders, use:

ZINTERSTORE tmp-john lb.113.distance.2014.10 demo.male WEIGHTS 1 0

and then do ZREVRANGE tmp-john 0 -1 withscores  to actually view the filtered leaderboard

Delete a Key (applies to all data types)

DEL <key>

So to delete the “tmp-john” key that you created in the last example, use:  DEL tmp-john

and it’s gone.  There’s no “undo”.