Sorted Sets
Members ranked by score: leaderboards, priority queues, and time indexes
A sorted set (zset) is a set where every member has a numeric score, and members are kept ordered by score. It's arguably Redis's most distinctive data structure: ranking, range queries, and top-N in O(log N) are hard to do this cheaply anywhere else.
Commands
How it works
A large sorted set uses two structures together:
- a hash table from member → score, so
ZSCOREis O(1) - a skip list ordered by score, so ranks and ranges are O(log N)
A skip list is a sorted linked list with extra "express lane" pointers
at random heights, giving balanced-tree performance with much simpler
code. Each node also records how many elements its forward pointers
skip, which is what makes ZRANK O(log N) rather than O(N).
Small sorted sets (up to 128 members, values up to 64 bytes) use a listpack instead, ordered by score, which is smaller and just as fast at that size.
Leaderboards
The demo in labs/leaderboard.py builds a 10,000-player board:
docker compose -f compose_prod.yaml run --rm labs python leaderboard.py
top 5:
#1 player:7431 4488
...
player:4242: rank #4908 of 10000, score 1438
around me:
#4906 player:1155 1439
#4907 player:9449 1438
#4908 player:4242 1438 <-- you
memory used by the leaderboard: 852 KiBTop N, a player's rank, and the players around them are each one
O(log N) command. The same queries in SQL need an index on score and a
COUNT(*) of everyone above, which gets slow on large tables.
Ties are broken lexicographically by member name. In the output
above, three players have 1438 points and are ordered by name. If ties
should go to whoever scored first, encode time into the score, for
example score * 1e10 + (MAX_TS - timestamp). Scores are 64-bit
doubles, exact for integers up to 2^53.
Time-based indexes
Use a timestamp as the score, and a sorted set becomes an index by time:
This is how delayed job queues work (poll for due jobs, ZREM the ones
you take) and how the sliding-window rate limiter
keeps a log of recent request times.
Lexicographic ranges
When all scores are equal, members are ordered by name, and
ZRANGE ... BYLEX does prefix queries. That's a cheap autocomplete:
ZADD app:autocomplete 0 apple 0 apricot 0 avocado 0 banana
ZRANGE app:autocomplete [ap (aq BYLEX # members starting with "ap"
1) "apple"
2) "apricot"Pitfalls
ZRANGE key 0 -1 returns the whole set, and with millions of members
that's a blocking O(N) call and a huge reply. Always page with
LIMIT or bounded ranks. Likewise, ZUNIONSTORE over several large
sets is O(N) + O(M log M) and belongs in a background job, not a
request path.
Lab
- Run
leaderboard.py. Then inredis-cli,ZREVRANGE lb:weekly 0 2 WITHSCORESandZREVRANK lb:weekly player:4242. - Give player 4242 enough points to reach the top:
ZINCRBY lb:weekly 5000 player:4242, then check its rank again. - Check
OBJECT ENCODING lb:weeklyandOBJECT ENCODING app:products:by_price. Why are they different? - Build the autocomplete example and query for prefix
av.