Guides on the Redis Zset and How to Use it

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Redis provides DBAs, developers, and technical specialists with a variety of ways to save data and sort items. A few of these commands to use are lists, hashes, and sets. As you may know, sets resemble lists, except that with sets, hash tables are used to ensure that the strings in a set remain unique. Redis ZSET commands use hash tables too. And ZSET commands and hash commands have something in common but ZSET commands have additional sorting and fetching capabilities. Find out what they are by reading this guide that explains about Redis ZSET. Included are some examples detailing how to use the commands too. When finished, you may find ZSETS a worthwhile addition to your arsenal of Redis commands.

Prerequisites

  • Redis – Install the latest stable version for your OS.

  • Confirm your version number with the redis-cli --version command or the redis-server --version command.

What Is a Redis Zset

A Redis ZSET contains unique members (the keys) and scores (confined to values of floating-point numbers). The items in a ZSET are sorted and accessible by scores and the order by which the items in the ZSET were sorted. This differs from a hash because hashes can only be accessed by scores.

Begin working with this guide by opening Redis and creating a sample database. Let’s start with the first section below that shows what the Redis ZSET ZADD command can do.

Use the Redis Zadd Command

When you want to add unique members that have different scores and store them in your Redis database, use the ZADD Redis command like this:

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> ZADD "id" 1 "Mark"
(integer) 1
127.0.0.1:6379> ZADD "id" 2 "Arthur"
(integer) 1
127.0.0.1:6379> ZADD "id" 3 "Monica"
(integer) 1
127.0.0.1:6379> ZADD "id" 4 "Daniel"
(integer) 1
127.0.0.1:6379> ZADD "id" 5 "Sergio"
(integer) 1

Use the Redis Zrange Command

There may be times when you’ll want to fetch just one score value.

*Use the Redis ZSET command ZRANGE and identify that score by its order position.

1
2
127.0.0.1:6379> ZRANGE "id" 0 0
1) "Mark"

Use the Redis Zset to Get All the Values Stored

The ZRANGE ZSET command returns a range of scores sorted from low to high.

  • Use ZRANGE to display every score in a Redis ZSET like this:
1
2
3
4
5
6
127.0.0.1:6379> ZRANGE "id" 0 -1
1) "Mark"
2) "Arthur"
3) "Monica"
4) "Daniel"
5) "Sergio"

Use the Redis Zset With Score Option

  • You can also use the ZRANGE command to show all of the elements of a sorted ZSET using the WITHSCORES option as shown here:
1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> ZRANGE "id" 0 -1 WITHSCORES
 1) "Mark"
 2) "1"
 3) "Arthur"
 4) "2"
 5) "Monica"
 6) "3"
 7) "Daniel"
 8) "4"
 9) "Sergio"
10) "5"

Using the Redis Zrangebyscore Command

Fine-tune your results by inputting your desired members range by using the Redis ZSET command ZRANGEBYSCORE.

  • The ZRANGEBYSCORE command displays every element in a set based on a score range when you specify a minimum and maximum number as shown in this example here:
1
2
3
127.0.0.1:6379> ZRANGEBYSCORE "id" 0 2
1) "Mark"
2) "Arthur"
  • Try out the ZRANGEBYSCORE command with the WITHSCORES option like this:
1
2
3
4
5
127.0.0.1:6379> ZRANGEBYSCORE "id" 0 2 withscores
1) "Mark"
2) "1"
3) "Arthur"
4) "2"

Remove the Sorted Set Value Using the Command Zrem in Redis

Deleting data that is no longer needed is a common maintenance task you perform on a regular basis.

  • Use the ZREM Redis ZSET command to delete a member and score in a ZSET like this:
1
2
3
4
127.0.0.1:6379> ZREM "id" "Mark"
(integer) 1
127.0.0.1:6379> ZREM "id" "Mark"
(integer) 0

In the example above, the ZREM command removed the ZSET item. You can tell because it looked for the member and score again and 0 was returned. This shows that you have successfully removed the item from the ZSET. If you look for the item again, a 0 will display in the results.

Conclusion

The usefulness of Redis ZSET commands makes it a valuable time saver for managing your Redis database. You can add, delete, and retrieve members and scores in sorted ZSETS with ease and by using ZADD, ZRANGE, ZRANGEBYSCORE, adding WITHSCORES options with ZRANGE or ZRANGEBYSCORE, and ZREM. Because you can sort by members and scores, ZSETS does a little more than your basic Redis hash command.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.