What if you could find your top 5 bestsellers instantly, no matter how many sales you have?
Why Top-N queries in Redis? - Purpose & Use Cases
Imagine you run a small online store and want to find the top 5 best-selling products from hundreds of items. You try to look through all sales records one by one on paper or in a simple list.
Going through every sale manually is slow and tiring. You might miss some products or count wrong. It's hard to keep track and update the list as new sales happen. This wastes time and causes mistakes.
Top-N queries let you quickly find the highest scoring items, like best sellers, using Redis commands. Redis sorts and keeps track of scores automatically, so you get the top results instantly without scanning everything.
Loop through all sales, count each product, sort counts, pick top 5ZREVRANGE sales_sorted_set 0 4 WITHSCORES
You can instantly see the most popular items or users, making fast decisions and improving your service.
A music app shows the top 10 trending songs right now by using Redis sorted sets to track play counts in real time.
Manual counting is slow and error-prone.
Top-N queries use Redis to sort and retrieve top items fast.
This helps you make quick, accurate decisions based on data.
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
