Redis sorted sets
Introduction to Redis sorted sets
A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include:
- Leaderboards. For example, you can use sorted sets to easily maintain ordered lists of the highest scores in a massive online game.
- Rate limiters. In particular, you can use a sorted set to build a sliding-window rate limiter to prevent excessive API requests.
- Update a real-time leaderboard as players' scores change:
> ZADD leaderboard:455 100 user:1 (integer) 1 > ZADD leaderboard:455 75 user:2 (integer) 1 > ZADD leaderboard:455 101 user:3 (integer) 1 > ZADD leaderboard:455 15 user:4 (integer) 1 > ZADD leaderboard:455 275 user:2 (integer) 0
user:2's score is updated in the final
- Get the top 3 players' scores:
> ZRANGE leaderboard:455 0 2 REV WITHSCORES 1) "user:2" 2) "275" 3) "user:3" 4) "101" 5) "user:1" 6) "100"
- What's the rank of user 2?
> ZREVRANK leaderboard:455 user:2 (integer) 0
ZADDadds a new member and associated score to a sorted set. If the member already exists, the score is updated.
ZRANGEreturns members of a sorted set, sorted within a given range.
ZRANKreturns the rank of the provided member, assuming the sorted is in ascending order.
ZREVRANKreturns the rank of the provided member, assuming the sorted set is in descending order.
See the complete list of sorted set commands.
Most sorted set operations are O(log(n)), where n is the number of members.
Exercise some caution when running the
ZRANGE command with large returns values (e.g., in the tens of thousands or more).
This command's time complexity is O(log(n) + m), where m is the number of results returned.
Redis sorted sets are sometimes used for indexing other Redis data structures. If you need to index and query your data, consider RediSearch and RedisJSON.
- Redis Sorted Sets Explained is an entertaining introduction to sorted sets in Redis.
- Redis University's RU101 explores Redis sorted sets in detail.