ZRANGEBYSCORE for score-based queries in Redis - Time & Space Complexity
When we use ZRANGEBYSCORE in Redis, we want to find items within a score range quickly.
We ask: How does the time to get these items grow as the data grows?
Analyze the time complexity of the following code snippet.
# Get all members with scores between 50 and 100
ZRANGEBYSCORE mysortedset 50 100
# Optionally limit results
ZRANGEBYSCORE mysortedset 50 100 LIMIT 0 10
This code fetches members from a sorted set whose scores fall between 50 and 100, optionally limiting the number of results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the sorted set to find members in the score range.
- How many times: Depends on how many members fall within the score range; Redis uses a skiplist to jump quickly.
As the sorted set grows, finding the start point is fast, but returning more results takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 to 5 steps to find start, plus up to 10 to return |
| 100 | About 5 to 7 steps to find start, plus up to 100 to return |
| 1000 | About 7 to 10 steps to find start, plus up to 1000 to return |
Pattern observation: Finding where to start grows slowly, but returning more results grows linearly with how many are returned.
Time Complexity: O(log n + m)
This means it takes a small amount of time to find where to start, plus time proportional to how many results you get back.
[X] Wrong: "ZRANGEBYSCORE always takes the same time no matter how many results it returns."
[OK] Correct: The time to find the start is fast, but returning many results takes longer because Redis must send each member back.
Understanding how Redis handles score-based queries helps you explain efficient data retrieval in real systems.
"What if we add a LIMIT clause to always return only 5 results? How would the time complexity change?"
Practice
ZRANGEBYSCORE do?Solution
Step 1: Understand the command purpose
ZRANGEBYSCOREis used to get members from a sorted set based on their scores.Step 2: Compare options with command behavior
Only It returns members in a sorted set within a given score range. correctly describes returning members within a score range.Final Answer:
It returns members in a sorted set within a given score range. -> Option DQuick Check:
ZRANGEBYSCORE = returns members by score range [OK]
- Confusing ZRANGEBYSCORE with ZREM (deletion)
- Thinking it returns rank instead of members
- Mixing it up with ZADD (adding members)
myzset including their scores?Solution
Step 1: Recall correct option for including scores
The correct keyword to include scores in output isWITHSCORES.Step 2: Check syntax correctness
ZRANGEBYSCORE myzset 10 20 WITHSCORES uses the exact correct syntax:ZRANGEBYSCORE myzset 10 20 WITHSCORES.Final Answer:
ZRANGEBYSCORE myzset 10 20 WITHSCORES -> Option CQuick Check:
WITHSCORES keyword includes scores [OK]
- Misspelling WITHSCORES
- Using WITHSCORE or SCORES instead
- Omitting WITHSCORES when scores are needed
myzset with members and scores: "a"=5, "b"=10, "c"=15, "d"=20, "e"=25
What is the result of
ZRANGEBYSCORE myzset 10 20 LIMIT 1 2?Solution
Step 1: Identify members in score range 10 to 20
Members with scores 10, 15, 20 are "b", "c", "d".Step 2: Apply LIMIT 1 2
LIMIT 1 2 means skip 1 member, then return next 2 members. Skipping "b", next two are "c", "d".Final Answer:
["c", "d"] -> Option AQuick Check:
LIMIT skips then returns count [OK]
- Ignoring LIMIT offset and returning from start
- Confusing score range with member count
- Including members outside score range
ZRANGEBYSCORE myzset (10 20 but get an error. What is the likely cause?Solution
Step 1: Analyze the command syntax
The command uses(10to indicate exclusive minimum score 10, which is correct syntax.Step 2: Identify the error
The command ends with20without closing parenthesis or space, causing syntax error.Final Answer:
Syntax error due to missing closing parenthesis after 20. -> Option AQuick Check:
Exclusive range needs parentheses on score values [OK]
- Forgetting parentheses for exclusive ranges
- Missing spaces between arguments
- Using parentheses incorrectly around scores
myzset, including their scores, but only if their score is greater than 50. Which command achieves this?Solution
Step 1: Define score range with exclusive lower bound
To get scores greater than 50, use(50for exclusive minimum.Step 2: Include scores and limit results
UseWITHSCORESto include scores andLIMIT 0 3to get top 3 members.Step 3: Verify command syntax
ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 correctly usesZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3.Final Answer:
ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 -> Option BQuick Check:
Exclusive range + WITHSCORES + LIMIT = correct command [OK]
- Using inclusive 50 instead of exclusive
- Placing LIMIT before WITHSCORES
- Using wrong bracket types for exclusive range
Ready to prove it?
Pass the test and this pattern moves unknown
