7.Redis管道/流水线

在这里插入图片描述

(Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务。一个请求会遵循以下步骤:
1 .客户端向服务端发送命令分四步(发送命令→命令排队→命令执行→返回结果),并监听Socket返回,通常以阻塞模式等待服务端响应。
2. 服务端处理命令,并将结果返回给客户端。
上述两步称为:Round Trip Time(简称RTT,数据包往返于两端的时间),问题笔记最下方)

在这里插入图片描述

如果同时需要执行大量的命令,那么就要等待上一条命令应答后再执行,这中间不仅仅多了RTT(Round Time Trip),而且还频繁调用系统IO,发送网络请求,同时需要redis调用多次read()和write()系统方法,系统方法会将数据从用户态转移到内核态,这样就会对进程上下文有比较大的影响了,性能不太好,o(╥﹏╥)o

是什么?

(管道(pipeline)可以一次性发送多条命令给服务端,服务端依次处理完完毕后,通过一条响应一次性将结果返回,通过减少客户端与redis的通信次数来实现降低往返延时时间。pipeline实现的原理是队列,先进先出特性就保证数据的顺序性。)

在这里插入图片描述

Redis pipelining

在这里插入图片描述

案例演示:

[atguigu@hadoop102 bin]$ vim cmd.txt
[atguigu@hadoop102 bin]$ sudo vim cmd.txt
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli 
OK
OK
(error) ERR syntax error
(error) ERR syntax error
(error) ERR syntax error
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli -a 123456 --pipe
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli -root 123456 --pipe
Unrecognized option or bad number of args for: '-root'
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli  123456 --pipe
(error) ERR unknown command '123456', with args beginning with: '--pipe' 
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli  -a 123456 --pipe
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
[atguigu@hadoop102 bin]$ cat cmd.txt | redis-cli  --pipe
All data transferred. Waiting for the last reply...
ERR syntax error
ERR syntax error
ERR syntax error
Last reply received from server.
errors: 3, replies: 5
[atguigu@hadoop102 bin]$  redis-cli 
127.0.0.1:6379> get k100
"v100"
127.0.0.1:6379> hget k300 name
(nil)
127.0.0.1:6379> 

小总结

Pipeline与原生批量命令对比

在这里插入图片描述

Pipeline与事务对比

在这里插入图片描述

使用Pipeline注意事项

在这里插入图片描述
原子性:? -->失败了还继续做。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值