Redis Pipeline (Pipeline)

Source: Internet
Author: User

Redisis aCSmode ofTCP Server, use andhttpa similar request-response protocol. AClientcan be through aSocketThe connection initiates multiple request commands. After each request command is issuedClientit usually blocks and waitsRedisService Processing,RedisAfter processing the request command will return the result through the response message toClient. The basic communication process is as follows:

CLIENT:INCR xserver:1client:incr xserver:2client:incr xserver:3client:incr xserver:4

Basically, four commands are required8aTCPmessage to complete. Network latency due to communication,if fromClientand theServerthe packet transfer time between the required0.125seconds. Then the four commands above8at least one message will be required1seconds to complete. So even ifRediscan handle every second -command, and ourClientyou can only issue four commands in one second. This shows that not fully utilizedRedisprocessing capacity. In addition to usingMget,msetsuch as a single command to handle multipleKeycommand, we can also usePipelinethe way fromClientpacking multiple commands together, without waiting for the response of a single command to return, andRediswhen the server finishes processing multiple commands, the processing results of multiple commands are packaged together and returned to the client. The communication process is as follows:

CLIENT:INCR xclient:incr xclient:incr xclient:incr xserver:1server:2server:3server:4

suppose not becauseTCPThe message is too long and is split. Maybe twoTCPthe message will be able to complete four commands,ClientYou can add fourincrcommand to put in aTCPmessages sent together,ServerYou can put the results of four commands into aTCPmessage returned. ThroughPipelinemode when there is a large amount of operation. We can save a lot of time originally wasted on network latency. It is important to note thatPipelineWay Package Command sent,Redisall command processing results must be cached before all commands are processed. The more commands are packaged, the more memory is consumed by the cache. So the more you pack the more commands the better. Specific how much appropriate needs to be tested according to the specific situation. Here's aJedisClient usesPipelinethe test:

package com.jd.redis.client; import redis.clients.jedis.jedis;import  redis.clients.jedis.pipeline; publicclass pipelinetest {     /**      *  @param  args     */     Publicstaticvoid main (String[] args)  {                int count = 1000;                long start = system.currenttimemillis ();         withoutpipeline (count);         long end = system.currenttimemillis ();         System.out.println ("withoutpipeline: "  +  (end-start));                 Start = system.currenttimemillis ();         usepipeline ( count);         end = system.currenttimemillis ();         system.out.println ("usepipeline: "  +  (end-start));            }     privatestaticvoid  withoutpipeline (int count) {        jedis jr =  null;        try {             jr = new jedis ("10.10.224.44",  6379);             for (int i =0; i<count; i++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;JR.INCR (" TestKey1 ");     &nBsp;       }        } catch   (exception e)  {             E.printstacktrace ();        }         finally{            if (jr!=null) {                 jr.disconnect ( );            }         }    }       privatestaticvoid  Usepipeline (Int count) {        jedis jr = null;         try {             jr = new&nbsp Jedis ("10.10.224.44",  6379);             pipeline pl = jr.pipelined ();             for (int i =0; i<count; i++) {         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PL.INCR ("TestKey2");             }                 pl.sync ();        } catch  (Exception  e)  {            e.printstacktrace ();         }        finally{             if (jr!=null) {         &Nbsp;       jr.disconnect ();             }        }    }}

Output:

withoutpipeline:11341usepipeline:344

The test result is still very obvious there is a big gap, so multiple operations withPipelineThere is still a clear advantage. I use it.Win7in theJedis Javathe client program connects to the LANLinuxon the virtual machineRedis Server.

Redis Pipeline (Pipeline)

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.