Updated February 18, 2023
Introduction to Redis ZSET
The Redis zset function is used to store the key type and value. In a redis, zset keys are unique, and values are limited by the number of floating point operations. The unique property in it is used to access the members. We can also access the items by using sorted orders and order score values. In redis, we can define the zset using two items.
Key Takeaways
- Redis zset will add all the members specified in the specified score to the order that is sorted at the key level.
- The items for the zset are accessible and sorted in redis by using the order scores from which we are sorting with it.
What is Redis ZSET?
Using it, we can add multiple scores and pairs. If the specified member is already a member of the sorted set, we can update the score when inserting the element into the correct position to ensure the ordering is correct.
If the key does not exist, a new sorted set with the specified members is created when the sorted set was empty. If a key exists but it does not contain the sorted set, an error will be returned. The score value in it should be represented by a string that contains floating point numbers.
How to Use Redis ZSET?
To use the redis zset, follow the steps below. To use it, we must first install the redis server on our system.
1. While installing the redis server now we need to start the redis server by using following command to use the zset.
Code:
redis-server
Output:
2. While starting the redis server for use with redis zset, we must connect the redis cli by using the following command. In the example below, we can see that it will connect to localhost and port 6379.
Code:
redis-cli
Output:
3. After connecting to the redis cli now in this step we are creating the set name as redis_zset. We are creating zset by using zadd command.
Code:
ZADD redis_zset 1 zset1
ZRANGE redis_zset 0 5 WITHSCORES
Output:
4. In below example we are adding new members to the already created dataset. We are adding new member name as zset2.
Code:
ZADD redis_zset 1 zset2
ZRANGE redis_zset 0 5 WITHSCORES
Output:
5. In below example we can see that it is now allowing duplicate members, it will return zero value as follows.
Code:
ZADD redis_zset 1 zset3
ZADD redis_zset 1 zset3
ZRANGE redis_zset 0 5 WITHSCORES
Output:
Redis ZSET Database
The redis zset database is defined at the time of adding members into the zset. We are adding single as well as multiple elements into the redis zset. At the time of adding elements, we need to define specified score which was need to define to the members. In below example we are adding two members at the time of creating zset. In below example we can see that we have created name as stud_name and added the members to the set as ABC and PQR.
Code:
ZADD stud_name 980 ABC 850 PQR
ZRANGE stud_name 0 5 WITHSCORES
Output:
In below, we are adding two members to an already created zset. As shown below, we have added CBD and BCD to the stud name zset group.
Code:
ZADD stud_name 980 CBD 850 BCD
ZRANGE stud_name 0 5 WITHSCORES
Output:
In below example we are adding single member to the existing zset. We are adding the member’s name as xyz as follows.
Code:
ZADD stud_name 1 XYZ
ZRANGE stud_name 0 5 WITHSCORES
Output:
Using the following command, we can retrieve data based on scores. The stud name is displayed in order in the example below.
Code:
ZREVRANGE stud_name 0 4
Output:
Redis ZSET Options
Redis zest supports multiple options, which we specified after the name of the key and before the score argument.
Below are the options as follows:
- XX – This option is used to update previously existing elements. It does not include any new elements.
- NX – This option is used to incorporate new elements. It will not update an element that already exists.
- LT – This options is used to update existing elements if suppose new score is smaller as compared to new score.
- GT – This option is used to update existing elements if suppose new score is larger as compared to new score.
- CH – This option is used to modify the value returned from the new elements that were added to the number of elements that already existed. Elements that we specified in the command line that have the same score that we are counting.
- INCR – At the time of specifying this options, it will act as a zincrby. We can specify single pair element by using these options.
Redis ZSET Command
Redis zset command contains the additional fetching and sorting capabilities. To use the zset we need to install the same in our system. It will contain the unique members and scores. At the time of adding unique members and different scores then we are using zadd command as follows.
Code:
ZADD "stud_id" 1 "ABC"
ZADD "stud_id" 2 "PQR"
ZADD "stud_id" 3 "XYZ"
ZRANGE "stud_id" 0 3
Output:
In below example we are using zrange command for identifying the score as per the position of order as follows.
Code:
ZRANGE "stud_id" 0 2
ZRANGE "stud_id" 0 -1
Output:
We can also use the zrange command to display all of the elements by using withscores, as shown below.
Code:
ZRANGE "stud_id" 0 2 WITHSCORES
ZRANGE "stud_id" 0 -1 WITHSCORES
Output:
We also use the zrangebyscore command to display each element that was set based on the range as shown below.
Code:
ZRANGEBYSCORE "stud_id" 0 1
ZRANGEBYSCORE "stud_id" 0 3 WITHSCORES
Output:
To remove the set of values, we use the zrem command. In the following example, we remove ABC from stud id as follows.
Code:
ZREM "stud_id" "ABC"
ZRANGE stud_id 0 5
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of redis zset?
Answer: The redis sorted sets are used in the 64-bit floating point number that represents the score. Zset inserts the number of scores specified into the sorted set key.
Q2. Which command we are using with redis zset?
Answer: We are using ZADD, ZRANGE, ZERM and ZRANGEBYSCORE command with redis zset, there are specified options for each command in it.
Q3. Which options we are using with redis zset?
Answer: We are using LT, XX, NX, GT, CH and INCR options at the time of using IT.
Conclusion
If the key does not exist, a new sorted set with the specified members is created when the sorted set was empty. It is used to hold key type and value. Keys are unique and values are limited by the number of floating point operations.
Recommended Articles
This is a guide to Redis ZSET. Here we discuss the introduction, how to use redis ZSET? options, command and FAQ. You may also have a look at the following articles to learn more –