EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Redis Tutorial Redis ZSET
 

Redis ZSET

Updated February 18, 2023

Redis ZSET

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Redis ZSET 1

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:

connect to localhost and port 6379

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:

Redis ZSET 4

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:

Redis ZSET 5

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:

now allowing duplicate members

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:

Redis ZSET 7

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:

adding two members

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:

adding single member

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:

retrieve data based on scores

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:

Redis ZSET 11

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:

Redis ZSET 12

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:

Redis ZSET 13

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:

Redis ZSET 14

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:

Redis ZSET 15

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 –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW