Redis (iii) Advanced Features: Piping

Source: Internet
Author: User

Redis is a responsive service that, when a client sends a request, is in a blocking state waiting for Redis to return results. Such a command consumes three parts: the time it takes to request from the client to the server, the time the result is from the server to the client, and the actual execution time of the command, the sum of the time consumed by the first two parts is called RTT (Round trip times), and when the client and the server have a network delay, The RTT can be very large, which can lead to performance problems. Pipeline (Pipeline) is to improve the situation, the use of pipelines, the client can send multiple requests at once without waiting for the response of the server, after all the commands are sent and then read the response of the service once, which can greatly reduce the RTT time and improve performance.


In the following example, the test of the INCR command is called 2000 times for a key locally, using the normal request and the pipeline.

public class App {public static void main (string[] args) {Long start = System.currenttimemillis (); Withoutpipeline (); System.out.println ("Without Pipeline takes:" + (System.currenttimemillis ()-start) + "Ms."); start = System.currenttimem Illis (); Withpipeline ();    System.out.println ("With Pipeline takes:" + (System.currenttimemillis ()-start) + "Ms.");        public static void Withpipeline () {Jedis Jedis = null;    try {Jedis = new Jedis ("localhost", 6379);    Jedis.flushdb ();                Pipeline p = jedis.pipelined ();                P.set ("Thekey", integer.tostring (0));        for (int i = 1; i <=; i++) {p.incr ("Thekey");                } response<string> r = P.get ("Thekey");                P.sync ();    System.out.println (R.get ());    } finally {jedis.close ();        }} public static void Withoutpipeline () {Jedis Jedis = null;    try {Jedis = new Jedis ("localhost", 6379);    Jedis.flushdb (); Jedis.set ("tHekey ", integer.tostring (0));        for (int i = 1; i <=; i++) {jedis.incr ("Thekey");    } System.out.println (Jedis.get ("Thekey"));    } finally {jedis.close (); }}}//output 2000Without Pipeline takes:183 ms.2000with Pipeline takes:47 Ms.

The results are very intuitive to reflect the difference between the two, to know that this is a local test, there is almost no network delay problem, if the test in different network segments, the effect will be more obvious. While the pipeline has somewhat improved performance, it is important to note that the return result of each command is cached on the server side first and then returned to the client at a time. If a bulk commit involves a large number of return results, it may lead to a memory overflow in the server, which is fatal in the production environment, how much batch processing is done, and it is best to make a reasonable assessment at the design stage.


Finally, the pipeline is only a solution, does not mean that at any time to use it as much as possible, but should be combined with consideration of network delay time, business involved in the volume of requests and so on, after all, the creation of the pipeline itself will be a certain amount of consumption.


Redis (iii) Advanced Features: Piping

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.