Updated February 18, 2023
Introduction to Redis ZADD
Redis is open-source and can be used as a database, message broker, or cache. Here there is a special option called Redis ZADD where the user can include all the given members with particular values that can be grouped as sorted sets and can be saved at the key. It can also be assigned with multiple values for pair of members.
Key Takeaways
- Using the ZADD command, if the member is added, a direct effect happens in the sorted set.
- The sorted set clutches a unique set of members and doesn’t admit to adding already present members into it. Instead, it starts to update the member’s value and place it in the right index to manage the proper order.
- If the key is not present, then ZADD is used to create a new sorted set and hold the given member.
- If the key is present but doesn’t hold any value, it throws an error.
What is Redis ZADD?
In Redis, the ZADD option includes all the given members with given values to the sorted set and saves that as a key. If the given member already has a position in the sorted set, the value is updated and the component is again corrected and updated to the right position to check the proper ordering. If there is no key, the new set with the given members is created, as if the allotted sorted set was null. If there is any key present that doesn’t hold any value in the sorted set, the error message is executed.
Redis Sorted Set
The sorted set in Redis has more advanced features than another set. The sorted set inhibits entire properties from the data structure of the Redis set. They manage the order of structured elements and every member of the sorted set has a value that is used to arrange the set values in ascending order.
To add new items in redis using zadd, follow the below syntax:
zadd course 1 "AWS"
Output:
Integer (1)
zadd course 1 “AWS” 3 “devops” 5”SQL” 6”DBA 9”ITIL
Output:
Integer (5)
To retrieve the data.
zrange course 0 2
Output:
AWS
Devops
SQL
To retrieve from end.
zrange course -1
Output:
DBA
zrange course 2 3 withscores
Output:
Devops 3
SQL 5
zscore course AWS
Output:
1
Redis ZADD Key
The definition of the ZADD key is discussed above and syntax is defined here.
$ Redis ZADD
The return value has an integer reply where the number of elements is included in the sorted set, but not including the elements that are present already for which the score keeps on updating. This feature is available since version 1.2.0 and here the time complexity is O(log(N)). It keeps on updating where every item addition. Here N is the number of items in the sorted set.
It works along with ACL categories like @fast, @sortedset, and @write.
The key adds all the given members with given values to the corresponding sorted set and saves it as a key and possible to include multiple values in multiple member pairs. The value should be represented as a string of floating point numbers with double precision. The valid values also include, -inf and +inf.
Redis ZADD Command
ZADD command supports multiple options, which can be specified after the key name and before the argument. The syntax and options are:
ZADD <name of the sorted_set_key> [NX | XX] [GT | LT] [CH] [INCR] <value> <member> [member’s value…]
XX, which supports only the updated items that is present already and doesn’t add any new elements.
NX, can add only new elements and doesn’t update all the present items.
LT, which can update the present items, and if it has new score values, it will be minimum than current values and the flag doesn’t hinder from adding new items.
GT can update existing items only if the new value is maximum than the current value. Here there is a flag that doesn’t stop from adding new items.
CH can alter the return value from new items added and returns the total items changed. Hence the change is abbreviated as CH. The changed items are new items added and the existing items for which the value is updated. So the specified items in the command line should have the same value and the past values should not be counted. It is important to note that, the ZADD command, returns the value of new counts added.
INCR is the option that behaves like ZINCRBY where the score-element pair is represented in this mode.
Here, the options NX, LT, and GT function as mutual.
The time complexity of the ZADD command is proportional to the logarithm and number of items. It acts more rapidly than other Redis commands.
Examples of Redis ZADD
As an example, the user has to manage the leaderboard for an online game that is played by hundreds of users across the world. In this game, every user can earn gold, once the successful mission is accomplished. The sorted set in Redis has an ideal data structure that is used for low-latency apps. So the user can create a sorted set that is identified by the key leaderboard. Few gamers will be included in a sorted set with different values, and every gold amount earned by the user can be mapped to the sorted set.
The syntax will be as follows, to include multiple members using ZADD.
ZADD leaderboard 750 gamer:1 345 gamer:2 230 gamer:3 170 gamer:4 200 gamer:5
ZADD course 1 AWS
Output:
(integer) 1
ZADD course 2 DEVOPS
Output:
(integer) 1
ZADD course 3 SQL
Output:
(integer) 1
ZRANGE course 0 -1
Output:
1) “AWS”
2) “DEVOPS”
3) “SQL”
ZRANGE course 0 -1 WITHSCORES
Output:
1) “AWS”
2) “1”
3) “DEVOPS”
4) “2”
5) “SQL”
6) “3”
FAQ
Given below are the FAQs mentioned:
Q1. What is the working of a sorted set in Redis ZADD?
Answer: The sorted set acts fast in updating, adding, and deleting their members and follows logarithmic time actions on every operation. As the members are arranged it can be accessed from the middle value efficiently. Hence the sorted set in ZADD is optimum for real-time scenarios like online gaming, low latency queues, and priority queues.
Q2. Can ZADD command be used in python?
Answer: Yes, as it follows the simple arranging order in a sorted set, it can be used in python which has a default syntax. It checks for the given member and value and places its corresponding index in a sorted set to manage it effectively.
Q3. How to fetch a member list in Redis?
Answer: Using the ZRANGE command, the member list is fetched along with the index, name, and values.
Conclusion
Few commands are present to work on sorted sets and in Redis, the ZADD command is implied to add one or more members with values to the sorted set and then save it as a key. Hence this is a brief note about Redis ZADD.
Recommended Articles
This is a guide to Redis ZADD. Here we discuss the introduction, redis ZADD key, command, examples, and FAQ respectively. You may also have a look at the following articles to learn more –