Redis communication protocol Resp

What is the redis communication protocol

RESP is the abbreviation of REdis Serialization Protocol, which is a serialization protocol specially designed for redis.
This protocol actually appeared in redis version 1.2, but it finally became the standard of redis communication protocol in redis2.0.

The format of redis communication protocol (RESP)

The Redis communication protocol is first divided into lines, and each line ends with \r\n line. Each line has a message header, which is divided into 5 types as follows:

+表示一个正确的状态信息,具体信息是当前行+后面的字符。
-表示一个错误信息,具体信息是当前行-后面的字符。
* 表示消息体总共有多少bulk行,不包括当前行,*后面是具体的行数。
$表示下一行数据长度,不包括换行符长度\r\n,后面则是对应的长度的数据。
:表示返回一个数值,:后面是相应的数字节符。

Example 1

The command "set aqin 1" is generally serialized into
*3\r\n$3\r\nset\r\n$4\r\naqin\r\n$1\r\n1\r\nOrdinary people can't understand, for
convenience To understand, format the above example

*3\r\n        -- 这个命令包含3个(bulk)字符串
$3\r\n        -- 第一个bulk string有3个字节
set\r\n       -- 第一个bulk string是set
$4\r\n        -- 第二个bulk string有4个字节
aqin\r\n      -- 第二个bulk string是agan
$1\r\n        -- 第三个bulk string有1个字节
1\r\n         -- 第三个bulk string是1

Executing it returns:

+OK\r\n 

Example 2

Command "get aqin":
*2\r\n$3\r\nget\r\n$4\r\naqin\r\nthat
is:

*2\r\n     -- 这个命令是2个bulk字符串的数组
$3\r\n     -- 第一个bulk字符串有3个字节: get
get\r\n
$4\r\n     -- 第二个bulk字符串有4个字节: aqin
aqin\r\n

testing and verification

Use telnet, the connection method is: After
the telnet
connection is successful, if redis has set a password, password authentication is also required. At this time, communication with redis has actually been established, and the redis command auth authentication can be used: auth command
:
keys

bodeMacBook-Pro:~ bo$ telnet xx.xx.xx.xx:xxxx
Trying xx.xx.xx.xx
Connected to xx.xx.xx.xx
Escape character is '^]'.
auth aqin
+OK
keys *
*13
$4
key9
$4
aqin
$4
key4
$4
key3
$4
key6
$4

Each command returns RESP format (\r\n is invisible, reflected as a newline).

Command: set aqin 1

set aqin 1
+OK

Command: get aqin

get aqin
$1
1

Use telnet to execute "get agan" in RESP format: *2\r\n$3\r\nget\r\n$4\r\naqin\r\n

*2
$3
get
$4
aqin 
$1
1

Guess you like

Origin blog.csdn.net/yaoyaochengxian/article/details/131505340