Redis learning notes-Pipeline

Source: Internet
Author: User
Redis is a CS-mode TCP server that uses a request response protocol similar to HTTP. A client can initiate multiple request commands through a socket connection. After each request command is sent, the client usually blocks and waits for the redis service to process it. After redis completes processing, the Request command returns the result to the client through the Response Message. The basic communication process is as follows:
Client: incr x
Server: 1
Client: incr x
Server: 2
Client: incr x
Server: 3
Client: incr x
Server: 4
Based on the above four commands, eight TCP packets are required. Due to network latency, it takes 0.125 seconds to transmit packets from the client to the server. It takes at least one second to complete the above four messages. In this way, even if redis can process 100 commands per second, our client can only issue four commands in one second. This shows that redis's processing capabilities are not fully utilized. In addition to using single commands such as mget and mset to process multiple keys
We can also use pipeline to Package Multiple commands from the client and send them together without waiting for the response of a single command, the redis server will pack the processing results of multiple commands and return them to the client after processing multiple commands. The communication process is as follows:

client: incr x
server: 1
Server: 2
server: 3
server: 4

false settings are not split because the TCP packet is too long. Two TCP packets may be able to complete four commands. The client can put the four incr commands into one TCP packet for sending together, and the server can put the processing results of the four commands into one TCP packet for returning. When a large number of operations are performed through pipeline. We can save a lot of time wasted on network latency. You must note that the command is packaged and sent in pipeline mode. redis must cache the processing results of all commands before processing all commands. The more packaged commands, the more memory consumed by the cache. So isn't it possible to pack as many commands as possible. Tests are required based on actual conditions. The following is a test of using pipeline on a jredis client
package jredisstudy;
Import Org. jredis. jredis;
Import Org. jredis. connector. connectionspec;
Import Org. jredis. ri. alphazero. jredisclient;
Import Org. jredis. ri. alphazero. jredispipelineservice;
Import Org. jredis. ri. alphazero. connection. defaultconnectionspec;
public class pipelinetest {
Public static void main (string [] ARGs) {
long start = system. currenttimemillis ();
usepipeline ();
long end = system. currenttimemillis ();
system. out. println (end-Start);

Start = system. currenttimemillis ();
Withoutpipeline ();
End = system. currenttimemillis ();
System. Out. println (end-Start );

}

Private Static void withoutpipeline ()
{
Try {
Jredis = new jredisclient ("192.168.56.55", 6379 );
For (INT I = 0; I <100000; I ++)
{
Jredis. incr ("Test2 ");
}
Jredis. Quit ();
} Catch (exception e ){
}
}

Private Static void usepipeline () {
try {
connectionspec spec = defaultconnectionspec. newspec ("192.168.56.55", 6379, 0, null);
jredis = new jredispipelineservice (SPEC);
for (INT I = 0; I <100000; I ++)
{< br> jredis. incr ("Test2");
}< br> jredis. quit ();
}catch (exception E) {
}< BR >}< br> output
103408 // pipeline used
104598 // not used

the test results are not very obvious. This should be related to my test environment. I connect to a virtual machine in windows. Low network latency. Therefore, the advantage of pipeline
is not obvious. If the network latency is small, you 'd better not use pipeline. In addition to increasing complexity, the performance improvement is not obvious.

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.