Range queries for scoring in Redis - Time & Space Complexity
When we ask Redis to find items within a score range, we want to know how the time it takes grows as more items are stored.
We are trying to see how the cost changes when the number of scored items increases.
Analyze the time complexity of the following Redis commands.
ZADD leaderboard 100 user1
ZADD leaderboard 200 user2
ZADD leaderboard 300 user3
ZRANGEBYSCORE leaderboard 150 350
This code adds users with scores to a sorted set and then queries users with scores between 150 and 350.
Look for repeated work inside the query.
- Primary operation: Redis scans the sorted set to find members with scores in the given range.
- How many times: It depends on how many items fall inside the score range.
As the total number of items grows, the time to find items in a score range grows mostly with how many items match the range.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About number of items in range (e.g., 3) |
| 100 | About number of items in range (e.g., 20) |
| 1000 | About number of items in range (e.g., 150) |
Pattern observation: The work grows with how many items are returned, not the total stored.
Time Complexity: O(log n + m)
This means Redis quickly finds the start point with a small cost, then spends time proportional to how many items match the score range.
[X] Wrong: "The query time grows only with the total number of items stored."
[OK] Correct: Redis uses a fast search to jump to the start of the range, so the main cost depends on how many items are in the range, not all items.
Understanding how range queries scale helps you explain how databases handle large data efficiently, a useful skill in many real-world projects.
"What if we asked for a range that includes almost all items? 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 filtered by score range.Step 2: Compare options with command behavior
Only "It returns members in a sorted set within a specified score range." correctly describes returning members within a score range.Final Answer:
It returns members in a sorted set within a specified score range. -> Option AQuick Check:
ZRANGEBYSCORE = returns members by score range [OK]
- Confusing ZRANGEBYSCORE with commands that add or delete members
- Thinking it returns count instead of members
- Assuming it sorts members by name instead of score
myzset?Solution
Step 1: Recall basic syntax of ZRANGEBYSCORE
The command format isZRANGEBYSCORE key min maxwhere min and max are numbers or special strings.Step 2: Analyze options for correct syntax
ZRANGEBYSCORE myzset 10 20 uses plain numbers 10 and 20 correctly. Options A and B use parentheses or brackets which mean exclusive or inclusive but are not needed here. ZRANGEBYSCORE myzset "10" "20" uses quotes which are unnecessary.Final Answer:
ZRANGEBYSCORE myzset 10 20 -> Option CQuick Check:
Syntax = ZRANGEBYSCORE key min max [OK]
- Using parentheses or brackets incorrectly for inclusive ranges
- Adding quotes around numeric scores
- Mixing up min and max order
myzset with members and scores: {"a":5, "b":10, "c":15, "d":20}, what is the result of ZRANGEBYSCORE myzset 10 20 WITHSCORES?Solution
Step 1: Identify members with scores between 10 and 20 inclusive
Members with scores 10, 15, and 20 are "b", "c", and "d" respectively.Step 2: Understand WITHSCORES option output
WITHSCORES returns members and their scores as alternating elements in the list.Final Answer:
["b", "10", "c", "15", "d", "20"] -> Option AQuick Check:
Members with scores 10-20 + WITHSCORES = ["b", "10", "c", "15", "d", "20"] [OK]
- Including members outside the score range
- Returning only members without scores
- Mixing order of members and scores
myzset?
ZRANGEBYSCORE myzset 5 15 WITHSCORE
Solution
Step 1: Check the option spelling
The correct option to include scores is WITHSCORES (plural), not WITHSCORE.Step 2: Verify other parts of the command
The score range 5 to 15 is correct, LIMIT is optional, and key names do not require quotes.Final Answer:
The option should be WITHSCORES, not WITHSCORE. -> Option BQuick Check:
Correct option spelling = WITHSCORES [OK]
- Misspelling WITHSCORES option
- Reversing min and max scores
- Adding unnecessary quotes around key
players with player names and their scores. You want to find all players with scores greater than 50 but less than or equal to 100, including their scores. Which command correctly achieves this?Solution
Step 1: Understand score range requirements
Scores must be greater than 50 (exclusive) and less than or equal to 100 (inclusive).Step 2: Use parentheses for exclusive and plain number for inclusive
In Redis,(50means greater than 50 (exclusive), and100means less than or equal to 100 (inclusive).Step 3: Check command syntax
ZRANGEBYSCORE players (50 100 WITHSCORES correctly uses(50and100with WITHSCORES option.Final Answer:
ZRANGEBYSCORE players (50 100 WITHSCORES -> Option DQuick Check:
Exclusive lower bound uses (number) syntax [OK]
- Using brackets [] which Redis does not support for range
- Not using parentheses for exclusive bounds
- Confusing inclusive and exclusive range syntax
Ready to prove it?
Pass the test and this pattern moves unknown
