当前位置:网站首页>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
边栏推荐
- [stl]list Simulation Implementation
- registration status: 204
- JDBC的API解析
- ActiveMQ -- JDBC code of persistent mechanism
- flink sql怎么持久化?
- 优炫数据库对数据的加密是如何做的?
- 整理 华为AP-3010DN_V2配置创建wifi
- Redis operation uses cursor instead of keys
- Software examination system architecture designer concise tutorial | software life cycle
- Oracle10g单实例数据库升级到哪个版本好,求建议
猜你喜欢

What is steel grating?

『怎么用』代理模式

Troubleshooting error: NPM install emojis list failed

Oracle10g单实例数据库升级到哪个版本好,求建议

全网最简约的sklearn环境配置教程(百分百成功)

Sticky.js page scrolling div fixed position plug-in

activemq--消息重试机制
![[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box](/img/5c/52ccc0583451eba15fec18adb1499e.png)
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box

Feiling ok1028a core board adapts to rtl8192cu WiFi module

centos更改mysql数据库目录
随机推荐
registration status: 204
Composition of the interview must ask items
Ranking of data results in MySQL
API健康状态自检
2022-7-14 JMeter simulates the login of different users for pressure test
[C language] dynamic memory management, flexible array
[buuctf-n1book][Chapter 2 advanced web]ssrf training
Redis operation uses cursor instead of keys
[common tools] obtain system status information based on psutil and gputil
activemq--死信队列
Kubedm introduction
What is steel grating?
activemq--可持久化机制之JDBC
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
activemq--可持久化机制
Opencv realizes simple face tracking
redis操作利用游标代替keys
整理 华为AP-3010DN_V2配置创建wifi
activemq--可持久化机制之AMQ
Activemq-- delayed delivery and scheduled delivery