Using ZRANK and ZREVRANK to Find Member Positions in Redis Sorted Sets
📖 Scenario: You are managing a leaderboard for a gaming app. Players earn points and are ranked in a Redis sorted set. You want to find the position of specific players in the leaderboard, both from the top (lowest score rank) and from the bottom (highest score rank).
🎯 Goal: Build a Redis sorted set with player scores, then use ZRANK and ZREVRANK commands to find the position of players in the leaderboard.
📋 What You'll Learn
Create a sorted set called game_leaderboard with exact score-player pairs.
Set a variable for the player name to check the rank.
Use ZRANK to find the player's rank from lowest to highest score.
Use ZREVRANK to find the player's rank from highest to lowest score.
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking users by points, or sorting items by score in real-time applications.
💼 Career
Understanding sorted sets and rank commands is essential for backend developers working with Redis to implement efficient ranking and scoring systems.
Progress0 / 4 steps
1
DATA SETUP: Create the sorted set with player scores
Use the Redis command ZADD to create a sorted set called game_leaderboard with these exact score-member pairs: 1500 "Alice", 3000 "Bob", 2250 "Charlie", 1800 "Diana", and 2700 "Eve".
Redis
Hint
Use ZADD followed by the sorted set name, then pairs of score and member.
2
CONFIGURATION: Set the player name to check rank
Create a variable called player and set it to the string "Charlie" to specify the player whose rank you want to find.
Redis
Hint
Assign the string "Charlie" to the variable player using Redis commands.
3
CORE LOGIC: Use ZRANK to find the player's rank from lowest to highest score
Use the Redis command ZRANK with game_leaderboard and Charlie to find the rank of "Charlie" from the lowest to highest score.
Redis
Hint
ZRANK returns the zero-based rank of the member in the sorted set ordered from lowest to highest score.
4
COMPLETION: Use ZREVRANK to find the player's rank from highest to lowest score
Use the Redis command ZREVRANK with game_leaderboard and Charlie to find the rank of "Charlie" from the highest to lowest score.
Redis
Hint
ZREVRANK returns the zero-based rank of the member in the sorted set ordered from highest to lowest score.
Practice
(1/5)
1. What does the Redis command ZRANK return when used on a sorted set?
easy
A. The total number of members in the sorted set
B. The zero-based rank of a member with scores in ascending order
C. The score of the member in the sorted set
D. The member with the highest score
Solution
Step 1: Understand what ZRANK does
ZRANK returns the position of a member in a sorted set ordered by score from lowest to highest.
Step 2: Identify the rank type
The rank is zero-based, meaning the lowest score member has rank 0.
Final Answer:
The zero-based rank of a member with scores in ascending order -> Option B
Quick Check:
ZRANK = ascending zero-based rank [OK]
Hint: ZRANK ranks ascending, starting at zero [OK]
Common Mistakes:
Confusing rank with score
Thinking rank starts at 1
Mixing ZRANK with ZREVRANK
2. Which of the following is the correct syntax to get the rank of member 'user1' in sorted set 'scores' using ZREVRANK?
easy
A. ZRANK user1 scores
B. ZREVRANK user1 scores
C. ZRANK scores user1
D. ZREVRANK scores user1
Solution
Step 1: Recall ZREVRANK command syntax
The syntax is: ZREVRANK key member
Step 2: Apply to given example
Here, key is 'scores' and member is 'user1', so command is ZREVRANK scores user1
Final Answer:
ZREVRANK scores user1 -> Option D
Quick Check:
ZREVRANK key member syntax [OK]
Hint: Command syntax: ZREVRANK key member [OK]
Common Mistakes:
Swapping key and member order
Using ZRANK instead of ZREVRANK
Adding extra parameters
3. Given a sorted set 'players' with members and scores: {"alice": 50, "bob": 70, "carol": 60}, what is the result of ZRANK players bob?
medium
A. 2
B. 1
C. 0
D. None (member not found)
Solution
Step 1: Sort members by ascending score
Sorted by score ascending: alice(50), carol(60), bob(70)
Step 2: Find zero-based rank of 'bob'
bob is third in order, so rank is 2 (0-based)
Final Answer:
2 -> Option A
Quick Check:
ZRANK ascending order rank = 2 [OK]
Hint: ZRANK counts from lowest score, zero-based [OK]
Common Mistakes:
Assuming rank is score
Confusing ascending with descending order
Using ZREVRANK result instead
4. You run ZREVRANK leaderboard "playerX" but get a null reply. What is the most likely reason?
medium
A. "playerX" is not a member of the sorted set "leaderboard"
B. The command syntax is incorrect
C. The sorted set "leaderboard" is empty
D. ZREVRANK only works on lists, not sorted sets
Solution
Step 1: Understand ZREVRANK null reply meaning
Null reply means the member does not exist in the sorted set.
Step 2: Check other options
Syntax is correct, empty set would also return null but question implies member missing, and ZREVRANK works only on sorted sets.
Final Answer:
"playerX" is not a member of the sorted set "leaderboard" -> Option A
Quick Check:
Null reply means member missing [OK]
Hint: Null reply means member not in set [OK]
Common Mistakes:
Thinking syntax error causes null
Assuming empty set returns error
Confusing data types for ZREVRANK
5. You have a sorted set 'scores' with members and scores: {"x": 10, "y": 20, "z": 30}. You want to find the rank of member 'y' counting from highest score first. Which command and expected result is correct?
hard
A. ZRANK scores y returns 1
B. ZREVRANK scores y returns 2
C. ZREVRANK scores y returns 1
D. ZRANK scores y returns 2
Solution
Step 1: Understand ZREVRANK ranks descending
ZREVRANK ranks members from highest to lowest score, zero-based.
Step 2: Sort members descending and find 'y' rank
Descending order: z(30), y(20), x(10). 'y' is second, so rank is 1.
Final Answer:
ZREVRANK scores y returns 1 -> Option C
Quick Check:
ZREVRANK descending zero-based rank = 1 [OK]
Hint: ZREVRANK ranks from highest score, zero-based [OK]
Common Mistakes:
Mixing ZRANK and ZREVRANK results
Counting rank starting at 1 instead of 0
Confusing ascending and descending order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.