Redis _ redis Pipeline

Source: Internet
Author: User
Tags redis version redis server

Redis _ redis Pipeline
Redis is a TCP server using the client-server model and what is called a Request/Response protocol.
Redis uses a tcp-based client-server model, which can also be calledRequest/ResponseProtocol model. This means that usually a request is accomplished with the following steps:
It means that a request is usually completed in the following steps:
The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.
The client initiates a query request to the redis server and connects to the redis server through socket. It is usually blocked before receiving the response.
The server processes the command and sends the response back to the client
The redis server executes the command and sends the result to the client So for instance a four commands sequence is something like this:
Therefore, the order in which rdis instances execute four commands is roughly as follows:
Client: INCR X
Server: 1
Client: INCR X
Server: 2
Client: INCR X
Server: 3
Client: INCR X
Server: 4 Clients and Servers are connected via a networking link. such a link can be very fast (a loopback interface) or very slow (a connection established over the Internet with your hops between the two hosts ). whatever the network latency is, there is a time for the packets to travel from the client to the server, and back from the server to the client to carry the reply.
The client and the redis server are connected over the network. Such a connection can be very fast (a local return port) or very slow (two hosts connected through the Internet ). However, regardless of the network factors, the server returns a response when a packet is sent to the server. (Explanation: the time when the request is sent to receive the response is RTT.) This Time is called RTT (Round Trip time ). it is very easy to see how this can affect the specified CES when a client needs to perform into requests in a row (for instance adding into elements to the same list, or populating a database with your keys ). for instance if the RTT time is 250 milliseconds (in the case of a very slow link over the Internet), even if the server is able to process 100 k requests per second, we'll be able to process at max four requests per second.
The time overhead of this process is RTT (a round-trip time ). obviously, performance is affected when a client continuously requests (adding many elements to the same list or getting many keys. Therefore, if the RTT time of a redis instance is 250mm (the network connection is slow), although redis can process K of requests per second theoretically, we can only process four requests per second. if the interface used is a loopback interface, the RTT is much shorter (for instance my host reports 0,044 milliseconds pinging 127.0.0.1 ), but it is still a lot if you need to perform into writes in a row.
If the network uses an interface, the RTT time will be much shorter (for example, if I ping the instance of the local machine as long as 44mm), but if the command is continuously sent, it will still be relatively long. Fortunately there is a way to improve this use case.
Fortunately, there are some improvement cases. redis PipeliningA Request/Response server can be implemented so that it is able to process new requests even if the client didn't already read the old responses. this way it is possible to sendMultiple commandsTo the server without waiting for the replies at all, and finally read the replies in a single step.
A Request/Response sever can execute a new requests, although the client does not receive the previous respones. This method sends many commands to the server and does not wait for the response until all the responses are read at the last time. This is called pipelining, and is a technique widely in use since includecades. For instance using POP3 protocol implementations already supported this feature, dramatically speeding up the process of downloading new emails from the server.
This method is called pipelining, a widely used technology more than a decade ago. For example, POP3 has implemented this feature, which greatly speeds up downloading emails from the server. Redis supports pipelining since the very early days, so whatever version you are running, you can use pipelining with Redis. This is an example using the raw netcat utility:
Redis already supports pipelining in the early days, so you can use pipelining in any version. The following is an example of using netcat:

$ (printf "PING\r\nPING\r\nPING\r\n"; sleep 1) | nc localhost 6379+PONG+PONG+PONG
This time we are not paying the cost of RTT for every call, but just one time for the three commands
Not every call takes an RTT time, but an RTT time to execute three commands. To be very explicit, with pipelining the order of operations of our very first example will be the following:
Obviously, the pipelining operation process is as follows:
Client: INCR X
Client: INCR X
Client: INCR X
Client: INCR X
Server: 1
Server: 2
Server: 3
Server: 4 important note: While the client sends commands using pipelining, the server will be forced to queue the replies, using memory. so if you need to send a lot of commands with pipelining, it is better to send them as batches having a reasonable number, for instance 10 k commands, read the replies, and then send another 10 k commands again, and so forth. the speed will be nearly the same, but the additional memory used will be at max the amount needed to queue the replies for this 10 k commands.
When the client uses pipelining to run some commands, the redis server uses the memory to create a queue for the response. Therefore, you use pipelining to run many commands. A good practice is to send messages in batches reasonably. For example, if a batch of 10 k is returned, 10 k occurs. The speed is probably the same, but the redis server will spend memory to create a response queue for this 10 K Command. Some benchmarkIn the following benchmark we'll use the Redis Ruby client, supporting pipelining, to test the speed improvement due to pipelining:
The following is an example of using ruby to support the pipelining client:
require 'rubygems'require 'redis'def bench(descr)    start = Time.now    yield    puts "#{descr} #{Time.now-start} seconds"enddef without_pipelining    r = Redis.new    10000.times {        r.ping    }enddef with_pipelining    r = Redis.new    r.pipelined {        10000.times {            r.ping        }    }endbench("without pipelining") {    without_pipelining}bench("with pipelining") {    with_pipelining}
Running the above simple script will provide the following figures in my Mac OS X system, running over the loopback interface, where pipelining will provide the smallest improvement as the RTT is already pretty low:
Running the above steps on mac OS x is compared with running without pipelining as follows:
without pipelining 1.185238 secondswith pipelining 0.250783 seconds
As you can see, using pipelining, we improved the transfer by a factor of five.
We can find that the pipelining transmission speed is increased by 5 times. Pipelining VS ScriptingUsing Redis scripting (available in Redis version 2.6 or greater) a number of use cases for pipelining can be addressed more efficiently using scripts that perform a lot of the work needed at the server side. A big advantage of scripting is that it is able to both read and write data with minimal latency, making operations like Read, compute, writeVery fast (pipelining can't help in this scenario since the client needs the reply of the read command before it can call the write command ).
A large number of experiments have proved that pipelining can handle more work on the server. One major advantage is that the step-by-step operation can reduce the latency of reading and writing, making reading, writing, and computing very fast (when the client needs to receive a response to send a command, pipelining cannot be met.) Sometimes the application may also want to send EVAL or EVALSHA commands in a pipeline. this is entirely possible and Redis explicitly supports it with the script load command (it guarantees that EVALSHA can be called without the risk of failing ).
Some applications may want to run EVAL or EVALSHA commands in pipeline. The use of the scriot load command is fully supported (it ensures that the execution has no risk of failure)

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.