Trending September 2023 # Adding One Or More Members With Values To Sorted Set # Suggested October 2023 # Top 18 Popular |

Trending September 2023 # Adding One Or More Members With Values To Sorted Set # Suggested October 2023 # Top 18 Popular

You are reading the article Adding One Or More Members With Values To Sorted Set updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Adding One Or More Members With Values To Sorted Set

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

To add new items in redis using zadd, follow the below syntax:

zadd course 1 "AWS"


zadd course 1 “AWS” 3 “devops” 5”SQL” 6”DBA 9”ITIL


Integer (5)

To retrieve the data.

zrange course 0 2





To retrieve from end.

zrange course -1



zrange course 2 3 withscores


Devops 3


zscore course AWS



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:

XX, which supports only the updated items that is present already and doesn’t add any new elements.

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


(integer) 1

ZADD course 2 DEVOPS


(integer) 1

ZADD course 3 SQL


(integer) 1

ZRANGE course 0 -1

1) “AWS”


3) “SQL”



1) “AWS”

2) “1”


4) “2”

5) “SQL”

6) “3”


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.


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 –

You're reading Adding One Or More Members With Values To Sorted Set

Update the detailed information about Adding One Or More Members With Values To Sorted Set on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!