What are Redis sorted sets and what problems do they solve?
Discover Redis sorted sets — unique members ranked by score with O(log N) inserts — and how they power leaderboards, priority queues, and rate limiting.
Expected Interview Answer
A Redis sorted set (ZSET) is a collection of unique members each attached to a floating-point score, kept automatically ordered by that score, giving fast ranked access and range queries.
Members stay unique like a set, but the score imposes an order maintained on every insert. Operations like ZADD, ZRANGE, ZRANGEBYSCORE, ZRANK, and ZINCRBY run in logarithmic time thanks to the underlying skip-list plus hash-table design. This makes ZSETs ideal for leaderboards, priority queues, rate limiting with sliding windows (scores as timestamps), time-series indexes, and any 'top N' or 'items between X and Y' query without re-sorting the data yourself.
- Automatic ordering by score with O(log N) inserts
- Fast rank and range queries (ZRANK, ZRANGEBYSCORE)
- Unique members prevent duplicates like a plain set
- Atomic score updates via ZINCRBY for live rankings
- Enables leaderboards, priority queues, and sliding-window rate limits
AI Mentor Explanation
A sorted set is like a live batting-average table: each batter appears once, and the moment a score updates the whole order rearranges instantly. You can instantly ask who sits in the top ten or who falls between averages of 40 and 50 without manually re-sorting the entire squad after every innings.
Step-by-Step Explanation
Step 1
Add members with scores
ZADD leaderboard 100 alice 90 bob adds unique members with numeric scores.
Step 2
Read ranked ranges
ZREVRANGE leaderboard 0 9 WITHSCORES returns the top ten by descending score.
Step 3
Query by score band
ZRANGEBYSCORE leaderboard 50 80 lists members whose scores fall in a range.
Step 4
Find a member's rank
ZRANK / ZREVRANK returns a member's position without scanning the whole set.
Step 5
Update scores atomically
ZINCRBY leaderboard 5 alice bumps a score and reorders instantly in one atomic step.
Step 6
Trim or expire entries
Use ZREMRANGEBYSCORE with timestamp scores to drop old items in sliding-window use cases.
What Interviewer Expects
- Defines a sorted set as unique members with scores kept ordered
- Knows core commands: ZADD, ZRANGE, ZRANGEBYSCORE, ZRANK, ZINCRBY
- Understands O(log N) complexity from the skip-list structure
- Can name real use cases like leaderboards and rate limiting
- Explains sliding-window rate limiting using timestamp scores
Common Mistakes
- Confusing a sorted set with a plain set that has no ordering
- Thinking members can be duplicated (only scores can repeat, members are unique)
- Assuming range queries require re-sorting the data manually
- Forgetting scores are floating-point, not just integers
- Overlooking ZINCRBY for atomic live-ranking updates
Best Answer (HR Friendly)
“A Redis sorted set keeps a list of unique items where each item has a number that decides its order, and the list stays sorted automatically. That makes it perfect for things like game leaderboards or 'top ten' lists where scores change constantly and you always want them ranked.”
Code Example
# Add players with scores
ZADD leaderboard 100 alice 90 bob 120 carol
# Top 3 by score (highest first)
ZREVRANGE leaderboard 0 2 WITHSCORES
# Players scoring between 90 and 110
ZRANGEBYSCORE leaderboard 90 110
# Alice's rank (0-based, highest first)
ZREVRANK leaderboard alice
# Award 15 points atomically -> auto re-ranks
ZINCRBY leaderboard 15 bobFollow-up Questions
- How would you build a sliding-window rate limiter with a sorted set?
- What data structures back a ZSET and why is that fast?
- How do ZRANK and ZREVRANK differ?
- How do you paginate a large leaderboard efficiently?
- How would you expire old entries in a time-series sorted set?
MCQ Practice
1. What makes a Redis sorted set different from a plain set?
A sorted set attaches a numeric score to each unique member and keeps them ordered by that score.
2. Which command returns members whose scores fall in a range?
ZRANGEBYSCORE returns members whose scores lie between the given minimum and maximum.
3. What is the typical time complexity of adding a member to a sorted set?
ZADD is O(log N) thanks to the skip-list structure backing the sorted set.
Flash Cards
What is a Redis sorted set? — Unique members each with a score, kept ordered by that score.
Command to add scored members? — ZADD key score member [score member ...].
Range query by score? — ZRANGEBYSCORE key min max.
Complexity of ZADD? — O(log N) via the underlying skip list.
Classic use case? — Leaderboards, priority queues, and sliding-window rate limiting.