Redis 通讯协议的疑问
怪我咯
怪我咯 2017-04-21 10:57:48
0
3
625

关于通讯协议,见https://redis.readthedocs.org/en/latest/topic/protocol.html

1)命令 set mykey myvalue 对应 要发送到Redis的字符串(要转化为二进制数据)是

"*3\r\n$3\r\nset\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n"

2)命令 get mykey 对应字符串是 "*2\r\n$3\r\nget\r\n$5\r\nmykey\r\n"

3)最后得到Redis发回的响应是 "+OK\r\n$7\r\nmyvalue\r\n"

我的问题是,Redis这样的响应格式,是否意味者 客户端发完命令(需要得到返回

值的命令如get)后,必须要等待回应到达之后才能发送下一个命令? 这样对客户端来说 效率是否低了点?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
左手右手慢动作

This is the normal TCP streaming method.

阿神

Your "low efficiency for the client" is just your fantasy, right? Is there any data to support this? Without any data to support it, can it be judged correctly based on subjective feelings?

There is also the relationship between efficiency and business. Generally speaking, you will only start to consider efficiency issues when efficiency does not support business. Isn’t it a bit premature for you to consider this efficiency before you have done anything?

One final addition. . This is the definition in the tcp protocol. If there is anything unclear, http://en.wikipedia.org/wiki/Transmission_Control_Protocol go here to learn about the tcp specification. .

迷茫

Maybe this question becomes: Can redis only send one command in one request? will be better

Conclusion: No matter which redis request protocol is used, sending multiple commands in one request is supported


redis request protocol

Non-standard redis request protocol format (also known as Inline):

Command name Parameter 1 Parameter 2 ... Parameter N

set name diaocow

Standard redis request protocol format:

*<Number of parameters>CR LF
$<Number of bytes of parameter 1>CR LF <Data of parameter 1>CR LF
...
$<Number of bytes of parameter N>CR LF
<Data of parameter N>CR LF

*3rn$3rnsetrn$4rnnamern$7rndiaocowrn

So when we need to set the value of the key name to diaocow, we can use the above two protocol formats to complete it (interested readers can use the telnet or nc command to test it themselves)

How does the Redis request protocol support batch execution? (This is what everyone often calls pipeline mode)

For example, I need to execute the following two commands in batches:

  1. set name diaocow

  2. set country china

Then the data in a request will look like this (assuming we use the standard protocol): *3rn$3rnsetrn$4rnnamern$7rndiaocowrn*3rn$3rnsetrn$7rncountryrn$5rnchinarn

Then inside redis, it will be parsed like this, pseudo code:

def processInputBuffer(client): 
    while (len(client.querybuf) > 0): 
        # 获取命令名,参数个数,参数数组以及当前读取到querybuf位置 
        cmd, argc, argv, pos = parseCommandInfo(client.querybuf) 
        # 执行命令 
        processCommand(cmd, argc, argv) 
        # 切换到下一个命令 
        client.querybuf = client.querybuf[pos:-1] 

parseCommandInfo will be parsed according to different protocols: if the first byte is '*', the standard protocol will be used

Remarks: All redis clients implement the function of sending commands in batches, which is nothing more than sending multiple commands to redis according to the format we just mentioned. Interested readers can refer to the client in the language they are familiar with

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template