ZRANGE and ZREVRANGE for reading in Redis - Time & Space Complexity
When we use ZRANGE or ZREVRANGE commands in Redis, we want to know how the time it takes changes as the size of the sorted set grows.
We ask: How does reading a range of elements from a sorted set scale with its size?
Analyze the time complexity of the following Redis commands.
# Get elements from index 0 to 9 in ascending order
ZRANGE mysortedset 0 9
# Get elements from index 0 to 9 in descending order
ZREVRANGE mysortedset 0 9
These commands read a slice of 10 elements from a sorted set, either from smallest to largest or largest to smallest.
Look at what repeats when Redis executes these commands.
- Primary operation: Redis navigates to the starting index using the skip list structure in O(log N) time, then collects the requested range of M elements.
- How many times: O(log N + M) where N is the sorted set size and M is the number of elements requested.
Imagine the sorted set has many elements, but you only ask for a small slice.
| Input Size (N) | Approx. Operations (M=10) |
|---|---|
| 10 | About log(10)+10 ≈ 13 operations |
| 100 | About log(100)+10 ≈ 17 operations |
| 1000 | About log(1000)+10 ≈ 20 operations |
Pattern observation: The time grows logarithmically with N and linearly with M. For fixed small M, it is nearly independent of N.
Time Complexity: O(log(N)+M)
This means the time grows linearly with the number of elements you ask to read (M) and logarithmically with the total size of the sorted set (N).
[X] Wrong: "Reading a range from a large sorted set takes time proportional to the whole set size O(N)."
[OK] Correct: Redis uses skip lists to find the range start in O(log N) time and only processes the requested M elements.
Understanding how Redis commands scale helps you design fast data access patterns and answer questions confidently about performance.
What if we asked for the entire sorted set instead of a small range (e.g., ZRANGE mysortedset 0 -1)? How would the time complexity change? (O(log(N)+N) = O(N))
Practice
ZRANGE myset 0 2 do?Solution
Step 1: Understand ZRANGE command behavior
ZRANGE returns elements in ascending order by score, from the start index to the end index.Step 2: Apply the index range 0 to 2
Indexes 0 to 2 mean the first three elements with the lowest scores are returned.Final Answer:
Returns the first three elements with the lowest scores from the sorted set 'myset'. -> Option BQuick Check:
ZRANGE with 0 2 = first 3 lowest scored items [OK]
- Confusing ZRANGE with ZREVRANGE order
- Thinking it deletes elements
- Assuming it returns all elements unordered
Solution
Step 1: Identify command for highest scores first
ZREVRANGE returns elements from highest to lowest score.Step 2: Use index range 0 to 4 for top 5 elements
Indexes 0 to 4 select the first 5 elements in descending order.Final Answer:
ZREVRANGE scores 0 4 -> Option AQuick Check:
Top 5 highest = ZREVRANGE 0 4 [OK]
- Using negative indexes incorrectly with ZRANGE
- Confusing ZRANGE and ZREVRANGE
- Wrong index order like 5 0
ZRANGE players 0 1 WITHSCORES?Solution
Step 1: Sort elements by score ascending
Sorted order by score: Alice(10), Carol(15), Bob(20).Step 2: Select indexes 0 to 1 with scores
Indexes 0 and 1 correspond to Alice and Carol with their scores.Final Answer:
["Alice", "10", "Carol", "15"] -> Option DQuick Check:
ZRANGE 0 1 WITHSCORES = Alice and Carol with scores [OK]
- Mixing order of elements
- Forgetting WITHSCORES doubles output
- Assuming ZREVRANGE order
ZREVRANGE leaderboard 0 2 but get unexpected results. Which of these is a likely cause?Solution
Step 1: Check if sorted set exists and has elements
If 'leaderboard' is empty or missing, ZREVRANGE returns empty result.Step 2: Validate command and index usage
ZREVRANGE supports index ranges; 0 2 is valid; using ZRANGE instead would change order but not cause empty result.Final Answer:
The sorted set 'leaderboard' is empty or does not exist. -> Option CQuick Check:
Empty set causes empty ZREVRANGE output [OK]
- Thinking index range is invalid
- Confusing ZRANGE and ZREVRANGE effects
- Believing ZREVRANGE lacks index support
Solution
Step 1: Identify index range for middle 3 elements
Elements ranked 4th to 6th correspond to indexes 3, 4, 5 (0-based).Step 2: Use ZRANGE for ascending order and WITHSCORES to get scores
ZRANGE scores 3 5 WITHSCORES returns elements at indexes 3 to 5 with their scores.Final Answer:
ZRANGE scores 3 5 WITHSCORES -> Option AQuick Check:
Middle 3 elements = ZRANGE 3 5 WITHSCORES [OK]
- Using ZREVRANGE which reverses order
- Off-by-one errors in index range
- Confusing rank with index
Ready to prove it?
Pass the test and this pattern moves unknown
