Top-N queries in Redis - Time & Space Complexity
When we ask for the top N items in Redis, we want to know how the time to get those items changes as the data grows.
We want to understand how fast Redis can find the top scores or values as the list gets bigger.
Analyze the time complexity of the following code snippet.
# Add scores to a sorted set
ZADD leaderboard 100 "Alice"
ZADD leaderboard 200 "Bob"
ZADD leaderboard 150 "Carol"
# Get top 2 players by score descending
ZREVRANGE leaderboard 0 1 WITHSCORES
This code adds players with scores to a sorted set and then retrieves the top 2 players with the highest scores.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Retrieving the top N elements from the sorted set.
- How many times: The operation scans or accesses elements up to N times to return the top N results.
Getting the top N items takes time mostly based on N, not the total size of the set.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations to get top 10 |
| 100 | About 10 operations to get top 10 |
| 1000 | About 10 operations to get top 10 |
Pattern observation: The time depends on how many top items you want, not on how many total items are in the set.
Time Complexity: O(N)
This means the time to get the top N items grows linearly with N, regardless of the total data size.
[X] Wrong: "Getting top N items takes longer as the whole set grows bigger."
[OK] Correct: Redis uses a sorted set structure that lets it jump directly to the top scores, so the total size does not slow down fetching the top N.
Understanding how Redis handles top-N queries shows you know how to work with efficient data structures and can explain performance clearly.
"What if we asked for the bottom N items instead of the top N? How would the time complexity change?"
Practice
Solution
Step 1: Understand sorted set ordering
Redis sorted sets store elements with scores. To get highest scores, we want descending order.Step 2: Identify the correct command
ZREVRANGE returns elements in descending order by score, perfect for top N queries.Final Answer:
ZREVRANGE -> Option BQuick Check:
Top N highest = ZREVRANGE [OK]
- Using ZRANGE which returns lowest scores first
- Confusing ZADD (adds elements) with query commands
- Using ZREM which removes elements
players?Solution
Step 1: Understand index ranges in ZREVRANGE
Indexes start at 0. To get top 3, use 0 to 2 inclusive.Step 2: Choose correct command and range
ZREVRANGE returns highest scores first. So ZREVRANGE players 0 2 gets top 3.Final Answer:
ZREVRANGE players 0 2 -> Option CQuick Check:
Top 3 highest = ZREVRANGE players 0 2 [OK]
- Using ZRANGE which returns lowest scores
- Starting index at 1 instead of 0
- Using negative indexes incorrectly
scores with members and scores: {"alice": 50, "bob": 70, "carol": 60}, what is the result of ZREVRANGE scores 0 1 WITHSCORES?Solution
Step 1: Order members by descending score
Scores descending: bob(70), carol(60), alice(50).Step 2: Select top 2 with scores
ZREVRANGE 0 1 returns first two: bob and carol with their scores.Final Answer:
["bob", "70", "carol", "60"] -> Option DQuick Check:
Top 2 with scores = bob(70), carol(60) [OK]
- Mixing ascending and descending order
- Forgetting WITHSCORES returns pairs
- Selecting wrong index range
ZREVRANGE leaderboard 0 4 but get an empty list. What is the most likely cause?Solution
Step 1: Check command syntax validity
ZREVRANGE syntax is correct for sorted sets, so syntax is not the issue.Step 2: Understand empty result causes
If the sorted set does not exist or is empty, ZREVRANGE returns an empty list.Final Answer:
The sorted set leaderboard does not exist or is empty -> Option AQuick Check:
Empty result means no data in sorted set [OK]
- Assuming syntax error without checking data
- Thinking ZREVRANGE works on hashes
- Believing index out of range causes error instead of empty list
game_scores. Which command should you use?Solution
Step 1: Identify order for lowest scores
ZRANGE returns elements in ascending order by score, so lowest scores first.Step 2: Select correct range for top 5
Indexes 0 to 4 select first 5 elements, which are lowest scores.Final Answer:
ZRANGE game_scores 0 4 -> Option AQuick Check:
Lowest 5 scores = ZRANGE 0 4 [OK]
- Using ZREVRANGE which returns highest scores
- Using negative indexes incorrectly
- Confusing ascending and descending order
Ready to prove it?
Pass the test and this pattern moves unknown
