redis作者@antirez在其blogHow to take advantage of Redis just adding it to your stack中提到:“Similarly using sorted sets it is possible to implement priority queues easily.”。
本文將會探討下如何使用redis提供的sorted sets數據結構,構造高效率的優先級隊列。
什么是sorted sets
以下段落來自reids.io->Data types。
Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
With sorted sets you can add, remove, or update elements in a very fast way (in a time proportional to the logarithm of the number of elements). Since elements are taken in order and not ordered afterwards, you can also get ranges by score or by rank (position) in a very fast way. Accessing the middle of a sorted set is also very fast, so you can use Sorted Sets as a smart list of non repeating elements where you can quickly access everything you need: elements in order, fast existence test, fast access to elements in the middle!
In short with sorted sets you can do a lot of tasks with great performance that are really hard to model in other kind of databases.
With Sorted Sets you can:
- Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK. Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.
- Sorted Sets are often used in order to index data that is stored inside Redis. For instance if you have many hashes representing users, you can use a sorted set with elements having the age of the user as the score and the ID of the user as the value. So using ZRANGEBYSCORE it will be trivial and fast to retrieve all the users with a given interval of ages.
- Sorted Sets are probably the most advanced Redis data types, so take some time to check the full list of Sorted Set commands to discover what you can do with Redis!
使用sorted sets構造優先級隊列
sorted sets有如下三個命令:
1.ZADD key score member [score] [member]
以O(log(N))的復雜度,向集合中加入一個元素。如下所示:
redis 127.0.0.1:6379> ZADD "www.baidu.com" 1 "first_page" 2 "second_page" 3 "third_page" 3 "another_page" (integer) 4
2.ZREVRANGE key start stop [WITHSCORES]
以O(log(N)+M)的復雜度,取元素。N是集合中元素個數,M是返回值的元素個數。使用WITHSCORES,將會同時返回對應元素的SCORE。在優先級隊列中,我們只取最高優先級的一個元素,如下所示:
redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 0 1) "third_page" redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 0 WITHSCORES 1) "third_page" 2) "3"
3.ZREM key member [member]
以O(log(N))的復雜度,刪除sorted set中的特定元素。這里的member為ZREVRANGE中的返回值即可,如下所示:
redis 127.0.0.1:6379> ZREM "www.baidu.com" "third_page" (integer) 1
刪除元素前后,優先級隊列中的元素對比如下:
redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 -1 WITHSCORES 1) "third_page" 2) "3" 3) "another_page" 4) "3" 5) "second_page" 6) "2" 7) "first_page" 8) "1" redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 -1 WITHSCORES 1) "another_page" 2) "3" 3) "second_page" 4) "2" 5) "first_page" 6) "1"
據此,一個高效(O(logN)的復雜度)的優先級隊列就可以使用了。
事件通知模式
參照上述方法構造的優先級隊列是非阻塞模式的,這樣,如果當前Sorted Sets為空,要求調用方不斷輪循(polling),這對使用者來說是非常不方便的。redis並未提供阻塞版本的ZREVRANGE,但是使用blpop命令,可以實現優先級隊列的阻塞語義。
參考:Pattern: Event notification
消費者(consumer)如下:
LOOP forever WHILE ZREVRANGE(key,0,0) returns elements ... process elements ... ZREM(key, elements) END BRPOP helper_key END
生產者(producer)如下:
MULTI
ZADD key element
LPUSH helper_key x
EXEC