Sorted sets
In Redis, the sorted-set data type combines the characteristics of both Redis lists and sets. Similarly to those of a Redis list, a sorted set's values are ordered, and like a set, each value is assured to be unique. Of all the various data structures in Redis, the sorted set is the closest to a killer feature. The flexibility of a sorted set allows for multiple types of access patterns depending on the needs of the application. Using a single sorted set for a player's scores in a game both the top and the bottom players are easily fetched for a leaderboard by either the ZRANGE
or ZREVRANGE
Redis commands.
For sorted sets, the ZADD
command adds a member with a score to the sorted set. The time complexity of ZADD
is O(log(n))
, meaning that as the size of the sorted set increases, the rate of increase in the processing time is a constant. Therefore, the difference between adding a new member to a large sorted set is trivial; the difference between log(10000) ~ 9.21034037...