參考:http://www.runoob.com/redis/redis-pipelining.htmlhtml
Redis是一種基於客戶端-服務端模型以及請求/響應協議的TCP服務。這意味着一般狀況下一個請求會遵循如下步驟:redis
Redis 管道技術能夠在服務端未響應時,客戶端能夠繼續向服務端發送請求,並最終一次性讀取全部服務端的響應。ruby
查看 redis 管道,只須要啓動 redis 實例並輸入如下命令:性能
$(echo -en "PING\r\n SET runoobkey redis\r\nGET runoobkey\r\nINCR visitor\r\nINCR visitor\r\nINCR visitor\r\n"; sleep 10) | nc localhost 6379 +PONG +OK redis :1 :2 :3
以上實例中咱們經過使用 PING 命令查看redis服務是否可用, 以後咱們設置了 runoobkey 的值爲 redis,而後咱們獲取 runoobkey 的值並使得 visitor 自增 3 次。測試
在返回的結果中咱們能夠看到這些命令一次性向 redis 服務提交,並最終一次性讀取全部服務端的響應ui
管道技術最顯著的優點是提升了 redis 服務的性能。spa
在下面的測試中,咱們將使用Redis的Ruby客戶端,支持管道技術特性,測試管道技術對速度的提高效果。htm
require 'rubygems' require 'redis' def bench(descr) start = Time.now yield puts "#{descr} #{Time.now-start} seconds" end def without_pipelining r = Redis.new 10000.times { r.ping } end def with_pipelining r = Redis.new r.pipelined { 10000.times { r.ping } } end bench("without pipelining") { without_pipelining } bench("with pipelining") { with_pipelining }
從處於局域網中的Mac OS X系統上執行上面這個簡單腳本的數據代表,開啓了管道操做後,往返時延已經被改善得至關低了。ip
without pipelining 1.185238 seconds with pipelining 0.250783 seconds
如你所見,開啓管道後,咱們的速度效率提高了5倍。it