Redis Learning Note 7--redis Pipeline (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 from a single socket connection. After each request command is issued , the client usually blocks and waits for the Redis service to process, andThe result is returned to the client via a response message after Redis finishes processing the request . 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

Basically, four commands are required8 xTCP messages can be completed. Network latency due to communication, if fromclient and 0.125 seconds. Then the above four commands redis can process 100 commands, And our client can only issue four commands in a second. This shows that the Redis's processing power is not fully utilized. In addition to using a single command such as mget,mset to handle multiple key's command we can also use client pack multiple commands together, without waiting for the response of a single command to return, and redis server processes multiple commands and packages the results of multiple commands back to the client. The communication process is as follows:

CLIENT:INCR x
client:incr x
client:incr x
client:incr x
server:1
server:2
Server:3
server:4< /span>

tcp message is too long to be split. Maybe two TCP message can complete four commands, INCR command into a server can put the results of four commands into a TCP message returned. by pipeline mode 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 that the pipeline way to package the command sent, redis must cache all command processing results 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. Below is a jedis client uses

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 = +;

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++) {

JR.INCR ("TestKey1");

}

} catch (Exception e) {

E.printstacktrace ();

}

finally {

if (jr!=null) {

Jr.disconnect ();

}

}

}

privatestaticvoid usepipeline (int count) {

Jedis JR = null;

Try {

JR = new Jedis ("10.10.224.44", 6379);

Pipeline PL = jr.pipelined ();

for (int i =0; i<count; i++) {

PL.INCR ("TestKey2");

}

Pl.sync ();

} catch (Exception e) {

E.printstacktrace ();

}

finally {

if (jr!=null) {

Jr.disconnect ();

}

}

}

}

Output:

withoutpipeline:11341

usepipeline:344

The test results are still obvious there is a large gap, so many operations with pipeline still have a clear advantage. I'm using the Jedis Java client program in Win7 to connect to Redis Server on a local area network Linux virtual machine .

Source:Redis Learning note 7--redis Pipeline (pipeline)

Redis Learning Note 7--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.