Redis learning Manual (pipeline)

Source: Internet
Author: User

I. Request Response Protocol and RTT:

Redis is a typical TCP Server Based on the C/S model. During the communication between the client and the server, the client usually initiates a request first, and the server executes the corresponding task after receiving the request, finally, send the obtained data or processing results to the client in response. In this process, the client waits for the results returned by the server in blocking mode. See the following command sequence:
Client: incr x
Server: 1
Client: incr x
Server: 2
Client: incr x
Server: 3
Client: incr x
Server: 4
In each request and response process, we have to bear the extra overhead brought about by network transmission. We usually call this overhead RTT (round trip time ). Now we assume that the RTT for each request and response is 250 milliseconds, and our server can process KB of data within one second, the result is that our server processes up to four requests per second. How can we optimize this performance problem?

Ii. Pipelines ):

Redis has provided support for command pipelines in earlier versions. Before giving a specific explanation, we will first transform the above example of Synchronous Response to an asynchronous response method based on the command pipeline, so that you can have a better perceptual knowledge.
Client: incr x
Client: incr x
Client: incr x
Client: incr x
Server: 1
Server: 2
Server: 3
Server: 4
As shown in the preceding example, after sending a command, the client can continue to send the subsequent command instead of waiting for a response from the server immediately. After the command is sent, read the responses of all previous commands at one time. This saves the RTT overhead in the synchronization mode.
It should be noted that if the redis server finds that the client request is based on a pipeline, the server will store the response data of each command into the queue after receiving and processing the request, and then send it to the client.

Iii. Benchmark:

The following are test cases and test results from the official redis website. It must be noted that the test is based onLoopback (127.0.0.1)Therefore, RTT takes less time. If it is based on the actual network interface, the performance improvement brought about by the pipeline mechanism is even more significant.

 1       Require   '  Rubygems  ' 
2 Require ' Redis '
3
4 Def evaluate (descr)
5 Start = Time . Now
6 Yield
7 Puts " # {Descr }# {time. Now-start} seconds "
8 End
9
10 Def without_pipelining
11 R = redis. New
12 10000 . Times {
13 R. Ping
14 }
15 End
16
17 Def with_pipelining
18 R = redis. New
19 R. pipelined {
20 10000 . Times {
21 R. Ping
22 }
23 }
24 End
25
26 Partition (" Without pipelining " ){
27 Without_pipelining
28 }
29 Partition ( " With pipelining " ){
30 With_pipelining
31 }
32 // Without pipelining 1.185238 Seconds
33 // With pipelining 0.250783 Seconds
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.