Redis管道技术的使用

4,743 阅读3分钟

目录

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