Redis actually has a custom network communication protocol?

Redis actually has a custom network communication protocol?

[[385171]]

All network communications require both parties to comply with the same protocol in order to be interconnected. The Redis protocol makes a compromise between the following points:

  • Simple implementation
  • Quickly analyzed by computer
  • Has a certain degree of readability

Network Layer

Redis listens for incoming connections on TCP port 6379. When a client connects, the Redis server creates a TCP connection for it. Each Redis command or data transmitted between the client and the server ends with \r\n.

ask

Redis receives commands consisting of different parameters. Once a command is received, it will be processed immediately and responded to the client.

New Unified Request Protocol

The new unified protocol was introduced in Redis 1.2, and in Redis 2.0, it became the standard way to communicate with a Redis server.

In this unified protocol, all parameters sent to the Redis server are binary safe.

The following is the general form:

  1. *<number of arguments> CR LF
  2. $<number of bytes of argument 1> CR LF
  3. <argument data> CR LF
  4. ...
  5. $<number of bytes of argument N> CR LF
  6. <argument data> CR LF

Example:

  1. ➜ ~ nc localhost 6379
  2. keys *
  3. *2
  4. $18
  5. user :sign:5:202101
  6. $18
  7. seckill_vouchers:6

The above command looks like a single-quoted string, so you can see the exact value of each byte in the query:

  1. "*2\r\n$18\r\nuser:sign:5:202101\r\n$18\r\nseckill_vouchers:6\r\n"  

This format is also used in Redis responses. When replying in bulk, this format is used for each parameter. The actual unified request protocol is what Redis uses to return list items and calls Multi-bulk replies. It is just N different bulk replies prefixed with *\r\n, which is the number of parameters (bulk replies) that follow.

response

Redis replies to commands with different response types. It may check the reply type from the first byte sent by the server:

Single-line response

The first byte of the response will be +

  1. set java edge
  2. +OK

Error message

The first byte of the response is -

  1. keys*
  2. -ERR unknown command `keys*`, with args beginning with :

Integer

The first byte of the response will be:

Batch Response

The first byte will be $

  1. keys *
  2. *2
  3. $18
  4. user :sign:5:202101
  5. $18
  6. seckill_vouchers:6

Multiple batch responses

  1. The first byte of the response will be *

<<:  China Telecom: Continue to build a comprehensive 5G private network solution integrating "network edge cloud and service"

>>:  PAM4 and Coherent Technology in 100G DWDM Optical Modules

Blog    

Recommend

What is the difference between WiFi and Ethernet connections?

In today's networking world, Wifi and Etherne...

Huawei 5G Power enables a green and fully connected world

[Shenzhen, China, April 17] At the 2019 Huawei Gl...

Will 5G be the next disruptive technology?

The telecommunications industry likes to use the ...

How to stress test network traffic?

There are many network testing software. Today, w...

Master these 5 tips to deploy Wi-Fi 6 to achieve the best results

The Wi-Fi 6 standard (802.11ax) brings many excit...

As 5G price war begins, US operators also adopt "Internet thinking"

After taking the lead in the world in 5G network ...