Appearance
Communication Protocol
The author of Redis believes that the bottleneck in database systems typically lies not in network traffic but in the internal logic processing of the database itself. Therefore, even though Redis uses a bandwidth-wasting text protocol, it achieves extremely high access performance. With all data stored in memory and a single thread providing services, a single node can reach an astonishing 100,000 QPS under full CPU core utilization.
RESP (Redis Serialization Protocol)
RESP stands for Redis Serialization Protocol. It is a straightforward text protocol, valued for its simplicity of implementation and excellent parsing performance.
Redis divides transmitted structured data into five minimal unit types, each ending with the carriage return and newline characters \r\n.
- Single-line strings begin with a +.
- Multi-line strings start with a $, followed by the string length.
- Integer values start with a :, followed by the integer's string representation.
- Error messages begin with a -.
- Arrays start with a *, followed by the array's length.
Examples include:
Single-line string "hello world":
+hello world\r\n
Multi-line string "hello world":
$11\r\nhello world\r\n
Integer 1024:
:1024\r\n
Error message "wrong type":
-WRONGTYPE Operation against a key holding the wrong kind of value\r\n
Array [1, 2, 3]:
*3\r\n:1\r\n:2\r\n:3\r\n
NULL is represented as a multi-line string with length -1:
$-1\r\n
An empty string is represented as a multi-line string with length 0:
$0\r\n\r\n
Note that there are two \r\n here, indicating an empty string.
Client to Server
Client commands are formatted as multi-line string arrays. For example, the simple set
command set author codehole
is serialized as follows:
*3\r\n$3\r\nset\r\n$6\r\nauthor\r\n$8\r\ncodehole\r\n
This string can be displayed in a more readable format:
*3
$3
set
$6
author
$8
codehole
Server to Client
Server responses support multiple data structures, making the response message structurally more complex, though still a combination of the five basic types.
Single-line string response:
127.0.0.1:6379> set author codehole
OK
Here, OK is a single-line response, not wrapped in quotes:
+OK
Error response:
127.0.0.1:6379> incr author
(error) ERR value is not an integer or out of range
This indicates a general error when trying to increment a string:
-ERR value is not an integer or out of range
Integer response:
127.0.0.1:6379> incr books
(integer) 1
Here, 1 is the integer response:
:1
Multi-line string response:
127.0.0.1:6379> get author
"codehole"
The string in quotes represents a multi-line string response:
$8
codehole
Array response:
127.0.0.1:6379> hset info name laoqian
(integer) 1
The command returns an array with the following structure:
*6
$4
name
$6
laoqian
$3
age
$2
30
$3
sex
$4
male
Nesting
The scan command is used to retrieve all key lists from the server, fetching them in cursor form:
127.0.0.1:6379> scan 0
1) "0"
2) 1) "info"
2) "books"
3) "author"
The scan command returns a nested array:
*2
$1
0
*3
$4
info
$5
books
$6
author
Summary
Although the Redis protocol contains many redundant carriage return and newline characters, this does not prevent it from being a highly popular text protocol in the field of internet technology. Many open-source projects use RESP as their communication protocol. In the tech realm, performance isn't everything; simplicity, understandability, and ease of implementation also require appropriate trade-offs.