(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注意事项
原子性:? -->失败了还继续做。