当前位置:网站首页>Detailed explanation of pipeline pipeline mechanism in redis
Detailed explanation of pipeline pipeline mechanism in redis
2022-07-25 09:19:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
One 、pipeline The background :
redis The client executes a command 4 A process :
dispatch orders -〉 Order to line up -〉 Command execution -〉 Return results This process is called Round trip time( abbreviation RTT, Round trip time ),mget mset It effectively saves RTT, But most orders ( Such as hgetall, did not mhgetall) Batch operation is not supported , Need to consume N Time RTT , At this time, we need pipeline To solve this problem .
Two 、pepeline Performance of
1、 not used pipeline perform N Bar command
2、 Used pipeline perform N Bar command
3、 Performance comparison between the two
Summary : This is the data from a set of statistical data , Use Pipeline The execution speed is faster than one by one , In particular, the greater the network delay between the client and the server , The more performance, the more physical .
3、 ... and 、 Native batch command (mset, mget) And Pipeline contrast
1、 Native batch commands are atomic ,pipeline It's nonatomic
( The concept of atomicity : A transaction is an indivisible minimum unit of work , Both succeed or fail . Atomic operation means that one of your business logic must be indivisible . Deal with one thing or succeed , Or they all failed , An atom is indivisible )
2、 Native batch command: one command is more than one key, but pipeline Support multiple commands ( There is a business ), Non atomicity
3、 The native batch command is implemented on the server side , and pipeline It needs to be done by both the server and the client
Four 、Pipeline Use it correctly
In the following code , I used an array of user names , Of array elements key The value is corresponding to the user id, Once the user changes his user name , I will modify two redis value :
- The number of user name changes of the current user needs +1
- Update the user name data corresponding to the current user
$redis = new Redis();
// Turn on pipeline mode , The representative temporarily puts the operation command in the pipeline
$pipe = $redis->multi(Redis::PIPELINE);
// Loop through the data , Perform the operation
foreach ($users as $user_id => $username)
{
// User name modification times +1
$pipe->incr('changes:' . $user_id);
// Update user name
$pipe->set('user:' . $user_id . ':username', $username);
}
// Start executing all commands in the pipeline
$pipe->exec();What operation is put in the pipe , There are no restrictions , Even if you put the operation of obtaining data ok Of . Now suppose we want to give someone redis key value +1, But get another redis key It's worth it value data . The following code is an operation to update the number of times a user is accessed , Another operation is to obtain user information data .
$redis = new Redis();
// Turn on pipeline mode
$pipe = $redis->multi(Redis::PIPELINE);
// Loop through the data , Perform the operation
foreach ($users as $user_id => $username)
{
// The number of times the user was visited +1
$pipe->incr('accessed:' . $user_id);
// Get user data records
$pipe->get('user:' . $user_id);
}
// Start executing all commands in the pipeline
$users = $pipe->exec();
// Print data
print_r($users);Be careful , Because every command in the pipeline will return data , So the final printed array , Will contain incr Records brought by operation , There are also those pulled down from getting user operations redis key Value is used as the index value of the print array .
But there is one advantage , The data returned by each operation command in the pipeline is stored in sequence in the pipeline ,key The value is 0,1,2 such . What data do we want , Just deal with it a little by yourself .
If we like to cancel the pipeline operation , Use the following code :
$pipe->discard();summary :pipeline Although easy to use , But every time pipeline The number of commands per assembly cannot be unlimited , Otherwise, one assembly pipeline Too much data , On the one hand, it will increase the waiting time of the client , On the other hand, it will cause some network congestion , It's possible to put a pipeline Split into several smaller pipeline To complete .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111449.html Link to the original text :https://javaforall.cn
边栏推荐
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
- uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec
- JDBC的api全解
- activemq--可持久化机制之JDBC
- 为什么说DAO是未来的公司形式
- Write two channel (stereo) immediately Wav file
- Rich text style word image processing
- [buuctf-n1book][Chapter 2 advanced web]ssrf training
- [deep learning] overview | the latest progress of deep learning
猜你喜欢

Unity ugui interaction (new ideas)

酷炫canvas动画冲击波js特效
![[BUUCTF-n1book][第二章 web进阶]SSRF Training](/img/29/8894d04b27e0e73c4458c27bd9b935.png)
[BUUCTF-n1book][第二章 web进阶]SSRF Training

为什么说DAO是未来的公司形式

将list集合的某一字段拼接单个String

Ten thousand words long, one word thoroughly! Finally, someone has made business intelligence (BI) clear

Feiling ok1028a core board adapts to rtl8192cu WiFi module

这家十年内容产业基建公司,竟是隐形的Web3先行者

Nacos搭建配置中心出现client error: invalid param. endpoint is blank

LabVIEW experiment - temperature detection system (experimental learning version)
随机推荐
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
神经网络学习(1)前言介绍
activemq--异步投递
ActiveMQ -- message retry mechanism
How does Youxuan database encrypt data?
Opencv realizes simple face tracking
Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)
全网最简约的sklearn环境配置教程(百分百成功)
[deep learning] overview | the latest progress of deep learning
学习周刊-总第 63 期-一款开源的本地代码片段管理工具
uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec
activemq--可持久化机制之JDBC的journal
API健康状态自检
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
2022-7-14 JMeter simulates the login of different users for pressure test
ActiveMQ -- dead letter queue
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
将list集合的某一字段拼接单个String
Silicon Valley classroom lesson 15 - Tencent cloud deployment
Asp. Net core CMD common instructions