Leaderboard implementation in Redis - Time & Space Complexity
When we build a leaderboard using Redis, we want to know how fast it can add scores and find rankings as more players join.
We ask: How does the work grow when the number of players grows?
Analyze the time complexity of the following code snippet.
ZADD leaderboard 1500 "player1"
ZADD leaderboard 2000 "player2"
ZADD leaderboard 1800 "player3"
ZRANK leaderboard "player2"
ZREVRANGE leaderboard 0 9 WITHSCORES
ZINCRBY leaderboard 100 "player1"
This code adds players with scores, finds a player's rank, lists top players, and updates a player's score.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorted set commands like ZADD, ZRANK, ZREVRANGE, and ZINCRBY that internally manage sorted data.
- How many times: Each command works on the sorted set of all players, which can be large.
As the number of players (n) grows, each operation takes more time but not equally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 x log(10) ≈ 8 |
| 100 | About 4 x log(100) ≈ 18 |
| 1000 | About 4 x log(1000) ≈ 28 |
Pattern observation: The work grows slowly as the number of players grows, roughly increasing with the logarithm of n.
Time Complexity: O(log n)
This means that adding or finding a player's rank takes a little more time as the leaderboard grows, but it grows slowly and stays efficient.
[X] Wrong: "Adding a new player or updating a score takes the same time no matter how many players there are."
[OK] Correct: Actually, Redis uses a sorted data structure that needs to keep things in order, so it does a bit more work as the list grows, but this extra work grows slowly, not instantly.
Understanding how Redis handles leaderboards helps you explain how data structures affect speed, a useful skill when discussing real-world systems.
"What if we used a simple list instead of a sorted set for the leaderboard? How would the time complexity change?"
Practice
Solution
Step 1: Understand leaderboard requirements
A leaderboard needs to store members with scores and retrieve them sorted by score.Step 2: Identify Redis data structure
Redis Sorted Sets store members with scores and keep them sorted automatically.Final Answer:
Sorted Set -> Option BQuick Check:
Leaderboard = Sorted Set [OK]
- Choosing List which is ordered by insertion, not score
- Using Hash which stores key-value but not sorted by score
- Using String which holds plain text, not multiple members
Solution
Step 1: Recall ZADD syntax
ZADD command syntax is: ZADD key score member.Step 2: Match syntax to options
ZADD leaderboard 100 user1 uses ZADD leaderboard 100 user1 which matches the correct syntax.Final Answer:
ZADD leaderboard 100 user1 -> Option AQuick Check:
ZADD syntax = ZADD key score member [OK]
- Swapping score and member order
- Using ZINCRBY which increments score, not sets it
- Using ZREM which removes member instead of adding
ZADD leaderboard 50 user1 ZADD leaderboard 100 user2 ZADD leaderboard 75 user3 ZREVRANGE leaderboard 0 1 WITHSCORESWhat is the output of the last command?
Solution
Step 1: Add members with scores
user1=50, user2=100, user3=75 added to sorted set.Step 2: Use ZREVRANGE 0 1 WITHSCORES
This fetches top 2 members by highest score descending: user2(100), user3(75).Final Answer:
["user2", "100", "user3", "75"] -> Option AQuick Check:
Top 2 by score desc = user2, user3 [OK]
- Confusing ZREVRANGE with ZRANGE (ascending order)
- Ignoring WITHSCORES option output format
- Mixing member and score order in output
ZADD leaderboard 30 user1 ZADD leaderboard 60 user2 ZADD leaderboard 45 user3 ZREVRANGE leaderboard 0 2But the output is empty. What is the most likely cause?
Solution
Step 1: Check command correctness
ZADD commands have correct syntax and add members with scores.Step 2: Analyze empty output from ZREVRANGE
Empty output means the key 'leaderboard' likely does not exist or was deleted before ZREVRANGE.Final Answer:
The leaderboard key was deleted before ZREVRANGE -> Option DQuick Check:
Empty output = missing key [OK]
- Thinking WITHSCORES is required for output (it's optional)
- Assuming ZREVRANGE returns ascending order
- Believing ZADD argument order was wrong (it was correct)
Solution
Step 1: Understand filtering by score
We want top 3 users with scores >= 50, sorted descending.Step 2: Choose correct Redis command
ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 returns members with scores between +inf and 50 descending, limited to 3.Final Answer:
Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 -> Option CQuick Check:
Filter by score with ZREVRANGEBYSCORE [OK]
- Using ZREVRANGE without score filtering
- Using ZRANGEBYSCORE which sorts ascending, not descending
- Filtering scores in app instead of Redis command
Ready to prove it?
Pass the test and this pattern moves unknown
