Pipeline of Redis Performance improvement

Source: Internet
Author: User

1, the normal use of the process before

The client sends a query to the server and reads from the socket, usually in a blocking manner, for the server response.
The server processes the command and sends the response back to the client.
Which means that every command will have a previous process.

2, the significance of the pipeline
If the successive Redis commands can be returned uniformly after the operation has been completed, the number of connections can be reduced, and the delay time will be reduced, and then the pipeline will be generated.
The basic implication of the pipeline is that the client can send multiple requests to the server without waiting for a reply and eventually reading the reply in one step.

3. Parameter Description:
Redis::multi or Redis::P ipeline. Default is Redis::multi
Redis::multi: Perform multiple operations as one transaction
Redis::P ipeline: Let (multiple) execute commands in a simple, faster way to send to the server, but without any guarantee of atomicity

Test Code 1:

<?phpset_time_limit (0); Ini_set (' Memory_limit ', ' 1024M '); $redis = new Redis (); G (' 1 '); $redis->connect (' 127.0.0.1 ');//not Atomic, pipe $redis->pipeline (); for ($i = 0; $i < 100000; $i + +) {$redis-&G    T;set ("test_{$i}", Pow ($i, 2)); $redis->get ("test_{$i}");} $redis->exec (); $redis->close (); G (' 1 ', ' e '); G (' 2 '); $redis->connect (' 127.0.0.1 ');//Things have atomic $redis->multi (); for ($i = 0; $i < 100000; $i + +) {$redis->set (    "Test_{$i}", Pow ($i, 2)); $redis->get ("test_{$i}");} $redis->exec (); $redis->close (); G (' 2 ', ' e ');//General G (' 3 '); $redis->connect (' 127.0.0.1 ');//Things have atomic for ($i = 0; $i < 100000; $i + +) {$redis->set ("Te    st_{$i} ", Pow ($i, 2)); $redis->get ("test_{$i}");} $redis->close ();    G (' 3 ', ' e '); function G ($star, $end = ") {Static $info = array ();        if (!empty ($end)) {$info [$end] = Microtime (true);        $sconds = $info [$end]-$info [$star];        Echo $sconds, "MS";    Echo Php_eol; } else {$info [$star] = Microtime (true); }}

Result output:
0.85941696166992ms 14.938266992569ms 15.121881961823ms

Test Code 2:

<?php$redis = new Redis();$redis->connect(‘127.0.0.1‘, 6379);$pipe = $redis->multi(Redis::PIPELINE);for ($i = 0; $i < 3; $i++) {    $key = "key::{$i}";    print_r($pipe->set($key, str_pad($i, 2, ‘0‘, 0)));    echo PHP_EOL;    print_r($pipe->get($key));    echo PHP_EOL;}$result = $pipe->exec();echo "<pre>";var_dump($result);

Output Result:
Redis object () Redis object () Redis object () Redis object () Redis object () Redis object ()
Array (6) {
[0]=>
BOOL (TRUE)
[1]=>
String (2) "00"
[2]=>
BOOL (TRUE)
[3]=>
String (2) "01"
[4]=>
BOOL (TRUE)
[5]=>
String (2) "02"
}

Pipeline of Redis Performance improvement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.