SpringBoot项目中Redis之管道技术

开发 项目管理 Redis
Redis 管道技术可以在服务端未响应时,客户端可以继续向服务端发送请求,并最终一次性读取所有服务端的响应。

[[389061]]

环境:springboot2.3.9.RELEASE + redis3.2.100

Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务。这意味着通常情况下一个请求会遵循以下步骤:

  • 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响应。
  • 服务端处理命令,并将结果返回给客户端。

Redis 管道技术

Redis 管道技术可以在服务端未响应时,客户端可以继续向服务端发送请求,并最终一次性读取所有服务端的响应。

Redis普通请求模型与管道请求模型对比

(普通请求模型)来源网络

RTT(Round-Trip Time),就是往返时延,在计算机网络中它是一个重要的性能指标,表示从发送端发送数据开始,到发送端收到来自接收端的确认(接收端收到数据后便立即发送确认),总共经历的时延。

 

一般认为,单向时延 = 传输时延t1 + 传播时延t2 + 排队时延t3

(管道请求模型)来源网络

性能对比

依赖

<dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-data-redis</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.commons</groupId> 
            <artifactId>commons-pool2</artifactId> 
</dependency> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

配置文件

spring: 
  redis: 
    host: localhost 
    port: 6379 
    password: ****** 
    database: 4 
    lettuce: 
      pool: 
        maxActive: 8 
        maxIdle: 100 
        minIdle: 10 
        maxWait: -1 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

普通方法

@Resource 
private StringRedisTemplate stringRedisTemplate ; 
public void execNormal() { 
        long start = System.currentTimeMillis() ; 
        for (int i = 0; i < 100_000; i++) { 
            stringRedisTemplate.opsForValue().set("k" + i, "v" + i) ; 
        } 
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + " ms") ; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

测试结果

总耗时:47秒左右

管道技术

public void execPipeline() { 
        long start = System.currentTimeMillis() ; 
        stringRedisTemplate.executePipelined(new RedisCallback<Object>() { 
            @Override 
            public Object doInRedis(RedisConnection connection) throws DataAccessException { 
                for (int i = 0; i < 100_000; i++) { 
                    connection.set(("pk" + i).getBytes(), ("pv" + i).getBytes()) ; 
                } 
                return null ; 
            } 
        }) ; 
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + " ms") ; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

测试结果

耗时:13秒左右

性能提升了3倍多。

完毕!!!

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-03-30 10:46:42

SpringBoot计数器漏桶算法

2020-10-27 14:15:42

SpringBoot

2020-09-23 07:39:59

SpringBoot项目Redis

2023-10-26 08:33:16

Redis管道技术

2022-06-20 10:45:55

SpringBoot项目

2021-03-19 10:14:28

SpringBoot项目异步调用

2023-08-23 13:24:00

异步编程方法

2024-07-03 13:03:30

Spring注解项目

2015-11-05 11:47:37

图片统计项目开发技术

2022-08-02 10:01:42

架构

2024-08-19 01:10:00

RedisGo代码

2009-08-19 16:36:29

C#管道技术

2021-07-06 21:30:06

Linux进程通信

2024-04-01 00:00:00

Redis缓存服务消息队列

2019-06-20 07:20:24

物联网项目物联网IOT

2021-09-30 10:45:33

Linux进程通信

2010-01-21 11:22:35

Linux多线程同步

2024-05-16 08:26:24

开发技巧项目

2024-09-10 09:05:12

SpringREST并发

2022-05-11 13:56:08

Java项目redis客户端
点赞
收藏

51CTO技术栈公众号