Redis 简明教程

Redis - Pipelining

Redis 是 TCP 服务器,支持请求/响应协议。在 Redis 中,通过以下步骤来完成请求:

Redis is a TCP server and supports request/response protocol. In Redis, a request is accomplished with the following steps −

  1. The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.

  2. The server processes the command and sends the response back to the client.

Meaning of Pipelining

Pipelining 的基本含义是,客户端可以向服务器发送多个请求,而无需等待任何回复,最后一步再读取回复。

The basic meaning of pipelining is, the client can send multiple requests to the server without waiting for the replies at all, and finally reads the replies in a single step.

Example

要检查 Redis Pipelining,只需启动 Redis 实例并在终端中输入以下命令。

To check the Redis pipelining, just start the Redis instance and type the following command in the terminal.

$(echo -en "PING\r\n SET tutorial redis\r\nGET tutorial\r\nINCR
visitor\r\nINCR visitor\r\nINCR visitor\r\n"; sleep 10) | nc localhost 6379
+PONG
+OK
redis
:1
:2
:3

在上面的示例中,我们将使用 PING 命令检查 Redis 连接。我们设置了一个名叫 tutorial 的字符串,其值为 redis 。稍后,我们获取键值并增加访问者数量三次。在结果中,我们可以看到所有命令均已提交到 Redis 一次,而 Redis 以单步提供了所有命令的输出。

In the above example, we will check Redis connection by using PING command. We have set a string named tutorial with value redis. Later, we get that keys value and increment the visitor number three times. In the result, we can see that all commands are submitted to Redis once, and Redis provides the output of all commands in a single step.

Benefits of Pipelining

此技术的好处是大幅提升了协议性能。Pipelining 带来的加速倍数范围从连接到 localhost 上的 5 倍,到慢速互联网连接上的至少 100 倍。

The benefit of this technique is a drastically improved protocol performance. The speedup gained by pipelining ranges from a factor of five for connections to localhost up to a factor of at least one hundred over slower internet connections.