For the past 2 years I’ve been all about Redis, and something I’m continually excited about is ZSET.

It is a sorted set implementation built into Redis. Its super fast, flexible, and I use it all the time, probably sometimes just because its fun.

I’ll show how it works, and outline two situations I’ve found it to be especially effective.

How is it used?

The simplest operation is to set a value for a given key. Imagine we want to record ages in a sorted set:

redis> ZADD "ages" 25 "john"
(integer) 1
redis> ZADD "ages" 30 "joe"
(integer) 1

Once they’re in, we can query them in order. To get the entries out, ordered by their rank:

redis> ZRANGE 'ages' 0 1
1) "john"
2) "joe"

It may be useful to see them in the reverse order (highest to lowest):

redis> ZREVRANGE 'ages' 0 1
1) "joe"
2) "john"

Or to have their scores returned alongisde them:

redis> ZREVRANGE 'ages' 0 1 WITHSCORES
1) "joe"
2) "30"
3) "john"
4) "25"

You can also do these ranges by score (ZRANGEBYSCORE), increment members (ZINCRBY), remove ranges (ZREMRANGEBYSCORE, ZREMRANGEBYRANK), or just query for scores (ZSCORE).

The cool part about this, is that these operations are so simple, that you can use them as persisted copies of data structures, that can exist between multiple services, or multiple hosts/nodes. In fact, that’s exactly what projects like redis-objects exist to make easy.

Use: Scoring things

This is probably the first thing most people think of with the sorted set. When something happens, we can bump up the value of a given key by a certain value and then query for the top values (or lowest values) easily using ZINCRBY, and efficiently.

Use: Queryable Dated Entries

Another use I come into pretty often is to make the values UNIX timestamps, and put data in the keys. With the set you can easily query for date ranges, get get the oldest and most recent entries, and easily purge ranges of results when they re no longer relevant (by date using ZREMRANGEBYSCORE, or by limit with ZREMRANGEBYRANK.

And then..

What’s your favorite use of ZSET?

Note on Documentation

One thing that’s really neat about the redis documentation is that they describe the runtime of each method right at the top. Check out the ZSET documentation for more details!