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