当前位置:网站首页>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 进阶区 hong
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
- 质量体系建设之路的分分合合
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Sobel filter
- Redis入门完整教程:GEO
- Sword finger offer 68 - I. nearest common ancestor of binary search tree
- Sword finger offer 68 - ii The nearest common ancestor of binary tree
- Li Kou 98: verify binary search tree
猜你喜欢
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
SPSS installation and activation tutorial (including network disk link)
【机器学习】手写数字识别
Advanced area a of attack and defense world misc Masters_ good_ idea
Advanced area of attack and defense world misc 3-11
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
攻防世界 MISC 进阶区 hong
Redis入门完整教程:事务与Lua
页面关闭前,如何发送一个可靠请求
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
随机推荐
特征缩放 标准化 归一化
Three stage operations in the attack and defense drill of the blue team
[Lua] Int64 support
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
攻防世界 MISC 进阶区 3-11
Postgresqlql advanced skills pivot table
MySQL Architecture - logical architecture
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
Erik baleog and Olaf, advanced area of misc in the attack and defense world
攻防世界 misc 高手进阶区 a_good_idea
Practice and principle of PostgreSQL join
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Logo special training camp Section IV importance of font design
SPSS installation and activation tutorial (including network disk link)
[cooking record] - stir fried 1000 pieces of green pepper
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Create Ca and issue certificate through go language
啃下大骨头——排序(二)
It is said that software testing is very simple, but why are there so many dissuasions?