Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use redis Pipeline pipeline

Shulou Source: shulou.com Published: 2022-06-02 00:00:36 11月10日 Update

Xiaobian to share with you how to use redis pipeline, I believe most people still do not know how to use, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Redis uses a client-server (CS) model and TCP servers for request/response protocols. This means that a request usually follows these steps:

The client sends a query request to the server and listens for Socket returns, usually in blocking mode, waiting for the server to respond.

The server processes the command and returns the result to the client.

Analysis of Common Mode and Pipeline Mode

Normal mode: Since there will be network latency for communication, suppose the packet transfer time between client and server takes 0.125 seconds. Then the above three commands and six messages take at least 0.75 seconds to complete. So even if redis can process 100 commands per second, our client can only issue four commands per second. This clearly underuses redis processing power.

Pipeline mode: (pipeline) can send multiple commands at a time and return the results at one time after execution. Pipeline reduces the round-trip delay time by reducing the number of communications between the client and redis, and the principle of Pipeline implementation is queue. The principle of queue is first-in-first-out, so as to ensure the sequence of data. The default number of synchronizations in Pipeline is 53, which means that when 53 pieces of data are accumulated in arges, the data will be submitted. The process is as shown in the following figure: client can put three commands into a tcp message to send together, server can put the processing results of three commands into a tcp message to return.

  Note that commands are sent in pipeline packages, and redis must cache the results of all commands before processing them. The more commands you pack, the more memory your cache consumes. So it's not about packing as many commands as possible. How much is appropriate needs to be tested on a case-by-case basis.

  

Case 1: Write 1 million pieces of data to redis//Generate 1 million pieces of data to the specified file declare(strict_types=1);//Enable strongly typed mode function random($length, $numeric = false){ $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35); $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed)); if ($numeric) { $hash = ''; } else { $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64); $length--; } $max = strlen($seed) - 1; for ($i = 0; $i

< $length; $i++) { $hash .= $seed{mt_rand(0, $max)}; } return $hash;}$filePath = './data.txt';for ($i = 0; $i connect('192.168.1.9', 6379); $redis->

auth ('****');//password authentication $redis->select(0);//select library $redis->pipeline();//Open pipeline foreach ($arr as $key => $value) { $redis->hsetNx('helloworld', (string)$key, $value); } $redis->exec(); echo $redis->hGet('helloworld', '1000000') . PHP_EOL; echo $redis->hGet('helloworld', '1000001') . PHP_EOL;} catch (\Exception $e) { echo $e->getMessage();} Case 2: Batch setting and reading through pipeline//Batch setting try { $redis = new \Redis(); $redis->connect('192.168.1.9', 6379); $redis->auth('******'); $redis->select(0); $redis->pipeline();//Open pipeline $redis->set('str1', 'h'); $redis->set('str2', 'e'); $redis->set('str3', 'l'); $redis->set('str4', 'l'); $redis->set('str5', 'o'); $redis->set('str6', 'w'); $redis->set('str7', 'o'); $redis->set('str8', 'r'); $redis->set('str9', 'l'); $redis->set('str10', 'd'); $result = $redis->exec(); print_r($result);} catch (\Exception $e) { echo $e->getMessage();} Result: Array( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9]=> 1)//read try { $redis = new \Redis(); $redis->connect('192.168.1.9', 6379); $redis->auth('******'); $redis->select(0); $redis->pipeline();//Open pipeline $redis->get('str1'); $redis->get('str2'); $redis->get('str3'); $redis->get('str4'); $redis->get('str5'); $redis->get('str6'); $redis->get('str7'); $redis->get('str8'); $redis->get('str9'); $redis->get('str10'); $result = $redis->exec(); print_r($result);} catch (\Exception $e) { echo $e->getMessage();} Result: Array( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => w [6] => o [7] => r [8] => l [9]=> d) The above is all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Commands pipes data patterns results processing services customers clients content messages articles more ordinary one-time ten thousand three principles situations files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Microsoft Xiaomi NVidia