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):

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

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!



0 条评论
登录 后参与评论


  • Externalizing Session State for a Spring Boot Application Using Spring-Session

    Spring-session is a very cool new project that aims to provide a simpler way of ...

  • kafka-connect-hive sink插件入门指南


  • 一步一步理解Impala query profile(二)

    在上一篇文章中,我们介绍了Impala query profie的概要部分,在本篇文章我们介绍Profile的查询计划(Query Plan)和执行概要(Exe...

  • GPIOs and Go

    Recently, I decided that I needed a small heads up display for import things I k...

  • Hack the box: Bastion

    In conclusion, Bastion is not a medium box. But it would be easier to solve this...

  • 用.NET Framework 2.0创建 Form设计器[翻译]

        这两天赶鸭子上驾,学习Form设计器,下面这篇在Msdn Magazine的文章可谓是经典,这两天学习了感觉还是做个翻译出来,更能够加强理解。对各位同...

  • Codeforces Round #408 (Div. 2)(A.水,B,模拟)

    A. Buying A House time limit per test:2 seconds memory limit per test:256 megaby...

  • C++/CLI(二)Mono C++/CLI Native调用和P/Invoke调用

    本文根据Mono C++原文档翻译,这篇文章的目的,就是想说CLR程序在VS下面生成的DLL不能给Unity调用,因为Mono的Native调用的编码和MS C...

  • Control is important! model predictive control mpc.pytorch lib

    Optimal control is a widespread field that involve finding an optimal sequence o...

  • linux到3.0了

    Yay! Let the bikeshed painting discussions about version numbering begin (or at...




玩转腾讯云 有奖征文活动