当前位置:网站首页>Redis入門完整教程:Pipeline
Redis入門完整教程:Pipeline
2022-07-04 22:52:00 【穀哥學術】
3.3.1 Pipeline概念
Redis客戶端執行一條命令分為如下四個過程:
1)發送命令
2)命令排隊
3)命令執行
4)返回結果
其中1)+4)稱為Round Trip Time(RTT,往返時間)。
Redis提供了批量操作命令(例如mget、mset等),有效地節約RTT。但
大部分命令是不支持批量操作的,例如要執行n次hgetall命令,並沒有
mhgetall命令存在,需要消耗n次RTT。Redis的客戶端和服務端可能部署在不
同的機器上。例如客戶端在北京,Redis服務端在上海,兩地直線距離約為
1300公裏,那麼1次RTT時間=1300×2/(300000×2/3)=13毫秒(光在真空中
傳輸速度為每秒30萬公裏,這裏假設光纖為光速的2/3),那麼客戶端在1秒
內大約只能執行80次左右的命令,這個和Redis的高並發高吞吐特性背道而
馳。
Pipeline(流水線)機制能改善上面這類問題,它能將一組Redis命令進
行組裝,通過一次RTT傳輸給Redis,再將這組Redis命令的執行結果按順序
返回給客戶端,圖3-5為沒有使用Pipeline執行了n條命令,整個過程需要n次
RTT。

圖3-6為使用Pipeline執行了n次命令,整個過程需要1次RTT。
Pipeline並不是什麼新的技術或機制,很多技術上都使用過。而且RTT
在不同網絡環境下會有不同,例如同機房和同機器會比較快,跨機房跨地區
會比較慢。Redis命令真正執行的時間通常在微秒級別,所以才會有Redis性
能瓶頸是網絡這樣的說法。
redis-cli的--pipe選項實際上就是使用Pipeline機制,例如下面操作將set
hello world和incr counter兩條命令組裝:
echo -en '*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n*2\r\n$4\r\nincr\r\
n$7\r\ncounter\r\n' | redis-cli --pipe
但大部分開發人員更傾向於使用高級語言客戶端中的Pipeline,目前大
部分Redis客戶端都支持Pipeline,第4章我們將介紹如何通過Java的Redis客
戶端Jedis使用Pipeline功能。
3.3.2 性能測試
錶3-1給出了在不同網絡環境下非Pipeline和Pipeline執行10000次set操作
的效果,可以得到如下兩個結論:
·Pipeline執行速度一般比逐條執行要快。
·客戶端和服務端的網絡延時越大,Pipeline的效果越明顯。

因測試環境不同可能得到的具體數字不盡相同,本測試Pipeline每次攜
帶100條命令。

3.3.3 原生批量命令與Pipeline對比
可以使用Pipeline模擬出批量操作的效果,但是在使用時要注意它與原
生批量命令的區別,具體包含以下幾點:
·原生批量命令是原子的,Pipeline是非原子的。
·原生批量命令是一個命令對應多個key,Pipeline支持多個命令。
·原生批量命令是Redis服務端支持實現的,而Pipeline需要服務端和客戶
端的共同實現。
3.3.4 最佳實踐
Pipeline雖然好用,但是每次Pipeline組裝的命令個數不能沒有節制,否
則一次組裝Pipeline數據量過大,一方面會增加客戶端的等待時間,另一方
面會造成一定的網絡阻塞,可以將一次包含大量命令的Pipeline拆分成多次
較小的Pipeline來完成。
Pipeline只能操作一個Redis實例,但是即使在分布式Redis場景中,也可
以作為批量操作的重要優化手段,具體細節見第11章。
边栏推荐
- 攻防世界 MISC 进阶区 3-11
- Redis入门完整教程:Bitmaps
- MD5 tool class
- 攻防世界 MISC 进阶区 hong
- 串口数据帧
- Detailed explanation of flask context
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- 剑指Offer 68 - II. 二叉树的最近公共祖先
- POM in idea XML dependency cannot be imported
- Redis入门完整教程:Redis Shell
猜你喜欢

攻防世界 MISC 进阶区 hit-the-core

Talk about Middleware
![[roommate learned to use Bi report data processing in the time of King glory in one game]](/img/06/22dde3fcc0456bd230e1d0cde339ec.png)
[roommate learned to use Bi report data processing in the time of King glory in one game]

Why is Dameng data called the "first share" of domestic databases?

Redis入门完整教程:客户端通信协议

Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1

Logo Camp d'entraînement section 3 techniques créatives initiales

攻防世界 MISC 进阶 glance-50

Unity修仙手游 | lua动态滑动功能(3种源码具体实现)

Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
随机推荐
[roommate learned to use Bi report data processing in the time of King glory in one game]
9 - 类
攻防世界 MISC 进阶区 Ditf
md5工具类
Redis入门完整教程:键管理
Hit the core in the advanced area of misc in the attack and defense world
【烹饪记录】--- 青椒炒千张
Sword finger offer 68 - I. nearest common ancestor of binary search tree
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Wake up day, how do I step by step towards the road of software testing
Create Ca and issue certificate through go language
Attack and defense world misc master advanced zone 001 normal_ png
Advanced area a of attack and defense world misc Masters_ good_ idea
攻防世界 MISC 进阶区 3-11
LOGO特训营 第二节 文字与图形的搭配关系
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Logo Camp d'entraînement section 3 techniques créatives initiales
Redis入门完整教程:GEO
High school physics: linear motion
繁华落尽、物是人非:个人站长该何去何从