Simple research on Redis communication protocol

1. Redis network communication protocol

The underlying network communication protocol of Redis is actually done through TCP.

2. Redis communication protocol

The communication protocol of Redis is firstly divided into lines, and each line ends with a \r\n line. Each line has a message header, and the message headers are divided into 5 types as follows:
(+) Indicates a correct status information, the specific information is the current line + the following characters.
(-) Indicates an error message, the specific information is the current line - the following characters.
(*) Indicates the total number of lines in the message body, excluding the current line, followed by the specific number of lines.
( \() means the data length of the next line, excluding the newline length \r\n,\) followed by the data of the corresponding length.
(:) means to return a value, followed by the corresponding number byte character.
for example:

*3\r\n  #消息一共有三行
$3\r\n #第一行有长度为3
set\r\n #第一行的消息
$4\r\n  #第二行长度为4 
demo\r\n #第二行的消息
$6\r\n #第三行长度为6
123456\r\n #第三行的消息
+OK\r\n #操作成功

3. Use Wireshark to trace Redis communication

I use a mac computer, the following are the operations under mac

3.1 Install redis

(1) Install directly using the command

brew install redis

(2) Start redis-server

Redis opens port 6379 by default

redis-server /usr/local/etc/redis.conf  #启动的时候指定配置文件

Effect picture:
redis-server

(3) Use redis-cli to connect to redis-server

Redis can be regarded as software of c/s architecture, and then open a terminal and enter the following command

redis-cli -h 192.168.0.102 -p 6379

Effect picture:
redis-cli
(4) Simple operation
operate

keys *  # 查看全部的可以
set demo 123 #设置k-v
TTL demo #查看demo的时间
flushall #清除全部的缓存
quit #退出客户端

3.2 Install wireshark

Wireshark is paid, you can find the trial version (cracked version) on the Internet, and it is recommended to buy the genuine version with conditions.

(1) Open wireshark:
wrieshark

I chose Loopback: lo0 in this place, because I installed redis locally. In fact, redis-server and redis-cli are both on 127.0.0.1. Only by choosing this method can the time server communicate with the client. If we connect to the redis-server on the remote server through redis-cli, we can choose WI-FI:en0 (this is the network card here, you can choose the corresponding network card according to your own computer)

(2) Track the communication when redis-cli connects to the service
We track the tcp stream
trace tcp stream
and get the following results
redis-cli.connection
. The operation of the connection is too complicated, and no detailed research has been done. Let's take a look at the simple set command.
redis-cli.set
It can be seen that it is consistent with the example given at the beginning of our article. Redis uses this communication protocol to communicate. After mastering the redis communication protocol, we can implement a redis client by ourselves, which is very simple.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667080&siteId=291194637