目錄
- Redis 管道技術(shù)
- SpringDataRedis 使用管道
- Redis 管道的性能測試
- 使用管道技術(shù)的注意事項
- 代碼示例
Redis 管道技術(shù)
Redis是一種基于客戶端-服務(wù)端模型(C/S模型)
以及請求/響應(yīng)協(xié)議
的TCP服務(wù)峭沦。
這意味著通常情況下一個請求會遵循以下步驟:
客戶端向服務(wù)端發(fā)送一個查詢請求聊闯,并監(jiān)聽Socket返回率碾,通常是以阻塞模式模燥,等待服務(wù)端響應(yīng)艳丛。
服務(wù)端處理命令胶坠,并將結(jié)果返回給客戶端燥撞。
這就是普通請求模型宴合。
所謂RTT(Round-Trip Time)投剥,就是往返時延师脂,在計算機網(wǎng)絡(luò)中它是一個重要的性能指標(biāo),表示從發(fā)送端發(fā)送數(shù)據(jù)開始江锨,到發(fā)送端收到來自接收端的確認(接收端收到數(shù)據(jù)后便立即發(fā)送確認)吃警,總共經(jīng)歷的時延。
一般認為啄育,單向時延 = 傳輸時延t1 + 傳播時延t2 + 排隊時延t3
為了解決這個問題酌心,Redis支持通過管道,來達到減少RTT的目的挑豌。
SpringDataRedis 使用管道
SpringDataRedis提供了executePipelined
方法對管道進行支持安券。
下面是一個Redis隊列的操作,放到了管道中進行操作氓英。
package net.ijiangtao.tech.framework.spring.ispringboot.redis.pipelining;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.Duration;
import java.time.Instant;
/**
* Redis Pipelining
*
* @author ijiangtao
* @create 2019-04-13 22:32
**/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class RedisPipeliningTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
private static final String RLIST = "test_redis_list";
@Test
public void test() {
Instant beginTime2 = Instant.now();
redisTemplate.executePipelined(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
for (int i = 0; i < (10 * 10000); i++) {
connection.lPush(RLIST.getBytes(), (i + "").getBytes());
}
for (int i = 0; i < (10 * 10000); i++) {
connection.rPop(RLIST.getBytes());
}
return null;
}
});
log.info(" ***************** pipeling time duration : {}", Duration.between(beginTime2, Instant.now()).getSeconds());
}
}
注意executePipelined
中的doInRedis
方法返回總為null
侯勉。
Redis 管道的性能測試
上面簡單演示了管道的使用方式,那么管道的性能究竟如何呢债蓝?
下面我們一起來驗證一下壳鹤。
首先,redis提供了redis-benchmark
工具測試性能饰迹,我在自己的電腦上通過cmd打開命令行芳誓,不使用管道,進行了一百萬次set和get操作啊鸭,效果如下:
$ redis-benchmark -n 1000000 -t set,get -q
SET: 42971.94 requests per second
GET: 46737.71 requests per second
平均每秒處理4萬多次操作請求锹淌。
通過-P
命令使用管道,效果如下:
$ redis-benchmark -n 1000000 -t set,get -P 16 –q
SET: 198098.27 requests per second
GET: 351988.72 requests per second
使用管道以后赠制,set和get的速度變成了每秒將近20萬次和35萬次赂摆。
然后我在服務(wù)器上挟憔,測試了使用SpringDataRedis進行rpop
出隊2000次的性能。
分別使用單線程出隊烟号、32個線程并發(fā)出隊和單線程管道出隊绊谭。下面是測試的結(jié)果:
從統(tǒng)計結(jié)果來看,出隊2000次汪拥,在單線程下大約需要6秒达传;32個線程并發(fā)請求大約需要2秒;而單線程下使用管道只需要70毫秒左右迫筑。
使用管道技術(shù)的注意事項
當(dāng)你要進行頻繁的Redis請求的時候宪赶,為了達到最佳性能,降低RTT脯燃,你應(yīng)該使用管道技術(shù)搂妻。
但如果通過管道發(fā)送了太多請求,也會造成Redis的CPU使用率過高辕棚。
下面是通過循環(huán)向Redis發(fā)送出隊指令來監(jiān)聽隊列的CUP使用情況:
當(dāng)管道中累計了大量請求以后欲主,CUP使用率迅速升到了100%,這是非常危險的操作坟募。
對于監(jiān)聽隊列的場景岛蚤,一個簡單的做法是當(dāng)發(fā)現(xiàn)隊列返回的內(nèi)容為空的時候,就讓線程休眠幾秒鐘懈糯,等隊列中累積了一定量數(shù)據(jù)以后再通過管道去取涤妒,這樣就既能享受管道帶來的高性能,又避免了CPU使用率過高的風(fēng)險赚哗。
Thread.currentThread().sleep(10 * 1000);