Redis管道技術的使用

目錄

Redis 管道技術

Redis是一種基於客戶端-服務端模型(C/S模型)以及請求/響應協議的TCP服務。

這意味着通常情況下一個請求會遵循以下步驟:

  • 客戶端向服務端發送一個查詢請求,並監聽Socket返回,通常是以阻塞模式,等待服務端響應。

  • 服務端處理命令,並將結果返回給客戶端。

這就是普通請求模型。

普通請求模型

所謂RTT(Round-Trip Time),就是往返時延,在計算機網絡中它是一個重要的性能指標,表示從發送端發送數據開始,到發送端收到來自接收端的確認(接收端收到數據後便立即發送確認),總共經歷的時延。

一般認爲,單向時延 = 傳輸時延t1 + 傳播時延t2 + 排隊時延t3

爲了解決這個問題,Redis支持通過管道,來達到減少RTT的目的。

通過管道減少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萬次。

然後我在服務器上,測試了使用SpringDataRedis進行rpop出隊2000次的性能。

分別使用單線程出隊、32個線程併發出隊和單線程管道出隊。下面是測試的結果:

管道出隊測試結果

從統計結果來看,出隊2000次,在單線程下大約需要6秒;32個線程併發請求大約需要2秒;而單線程下使用管道只需要70毫秒左右。

使用管道技術的注意事項

當你要進行頻繁的Redis請求的時候,爲了達到最佳性能,降低RTT,你應該使用管道技術。

但如果通過管道發送了太多請求,也會造成Redis的CPU使用率過高。

下面是通過循環向Redis發送出隊指令來監聽隊列的CUP使用情況:
監聽隊列的CUP使用情況

當管道中累計了大量請求以後,CUP使用率迅速升到了100%,這是非常危險的操作。

對於監聽隊列的場景,一個簡單的做法是當發現隊列返回的內容爲空的時候,就讓線程休眠幾秒鐘,等隊列中累積了一定量數據以後再通過管道去取,這樣就既能享受管道帶來的高性能,又避免了CPU使用率過高的風險。

Thread.currentThread().sleep(10 * 1000);

代碼示例

Github-ispringboot-redis

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章