You are currently browsing the See John Code posts tagged: redis

Redis ZSET

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 (ZREMRANGEBYSCOREZREMRANGEBYRANK), 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!

Hastebin Rocks

A few weeks ago, we had some sensitive data at work that we were passing around, and realized there was a real need for an open source pastebin software that could be installed behind a VPN. I liked the project, so I took it up for fun in my spare time. I wanted to keep the dependencies as small as possible, so I ended up going with anode.js server, backed by files and serving static assets.

The project is called haste-server, and there is a publicly available version The response to the project and the concept has been great, and I’ve seen a lot of people using it. Definitely check it out if you haven’t already, and be sure to also look at haste-client, a tiny CLI tool for uploading pastes to hastebin (or a private hastebin). Basic usage of haste-client is like:

$ cat something | haste | pbcopy # mac os
$ cat something | haste | xsel # linux 

After which, a URL with the output of STDOUT for cat something will be placed into your clipboard!

Read more on the about page and let me know what you think!