Skip to content

How to use Redis Sorted Lists

New Course Coming Soon:

Get Really Good at Git

A sorted set associates a rank to each item in a set.

Sorted sets work in a similar way to sets, and they use similar commands, except S is now Z, for example:

But they are slightly different.

ZADD accepts a score:

ZADD names 1 "Flavio"
ZADD names 2 "Syd"
ZADD names 2 "Roger"

As you can see, values must still be unique, but now they are associated to a score.

The score does not have to be unique.

Items in a set are always sorted by the score.

This is very useful to implement some kind of data storage tool like (usual example) a leaderboard. Or to indicate the time some item was added, with a timestamp.

You can get the score of an item using ZRANK:

ZRANK names "Flavio"

List all items in a sorted set using ZRANGE, which works similarly to LRANGE in lists:

ZRANGE names 0 -1

Add WITHSCORES to also return the scores information:

You can increment the score of an item in the set using ZINCRBY.

See all the sorted sets commands here.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!

Here is how can I help you: