RESP (REdis Serialization Protocal)
Redis A protocol for communication between a client and a server. It is simple, built on the TCP protocol, and provides a simple, high-performance, readable specification and semantics for data serialization.
Five data models
- Simple Strings
- Errors
- Integers
- Bulk Strings
- Array
Simple Strings
Simple Strings is used by the server in response to some client commands
Format: “+” at the beginning “\r\n” at the end, no ‘\r’ or ‘\n’ in the middle
# setThe command gets a response
127.0.0.1:6379> set name Foo
OK
#The server returns the full content:"+" OK "to \ r \ n"
#Exit the command127.0.0.1:6379 > quit
#The complete content returned by the server: +OK\r\n
Copy the code
The structure of Simple Strings is very Simple and not binary safe; So only simple content is returned on the server side. It’s just that the contents of GET, MGET, and so on are returned with Buik Strings.
Errors
Errors is used to return some error commands from the server to the client
Format: “-” beginning “\r\n” ending
#A command that does not exist was executed. Procedure
127.0.0.1:6379> gee name
(error) ERR unknown command 'gee'
#The server returns the full content:"-ERR unknown command 'gee'\r\n"
#Incorrect type operation
127.0.0.1:6379> set name Bob
OK
127.0.0.1:6379> zadd name 1 Foo
(error) WRONGTYPE Operation against a key holding the wrong kind of value
#The server returns the completed content:"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"
Copy the code
As you can see from the example, the middle part of the Erorrs response consists of two parts: the uppercase represents the error type, and the message represents the error details. That is – the Error message \ r \ n
Integers
For commands that need to return integer contents, such as INCR, LLEN, etc
Format :” :” beginning, \r\n ending, middle part must be a valid integer
# incr
127.0.0.1:6379> set count 1
OK
127.0.0.1:6379> incr count
(integer) 2
#The server returns the full content:":2\r\n"
# lpush llen del
127.0.0.1:6379> lpush userList Bob
(integer) 1
127.0.0.1:6379> llen userList
(integer) 1
#The server returns the full content:":1\r\n"Which represents the number of current element books in the list
# del
127.0.0.1:6379> del userList
(integer) 1
#The server returns the full content:":1\r\n""Indicates that the execution is successful
# exists
127.0.0.1:6379> set name Bob
OK
127.0.0.1:6379> exists name2
(integer) 0
#The server returns the full content:":0\r\n", meaning not exist
127.0.0.1:6379> exists name
(integer) 1
#The server returns the full content:":1\r\n"The meaning is existence
Copy the code
Each of these commands responds with Integers: SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, SCARD
Bulk Strings
Used to transmit binary secure character content. The maximum length is 512MB
Format: start with ‘$’, followed by an integer to represent the real character length, then \r\n, followed by the actual string contents, and finally \r\n, for example: “$6\r\nfoobar\r\n”
Return to normal
127.0.0.1:6379> set name foo
OK
127.0.0.1:6379> get name
"foo"
#The server returns the full content:"$3\r\nfoo\r\n"
Copy the code
Empty string return
127.0.0.1:6379> set name ""
OK
127.0.0.1:6379> get name
""
#The server returns the full content:"$0\r\n\r\n"
Copy the code
Null returns a key where GET does not exist
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)
#The server returns the full content:"$-1\r\n"
Copy the code
When the server returns Null Bulk Strings, the client library should return a Null object instead of Null characters. Empty string and nil are two meanings. Empty string means the key has a value of “” and nil means the key does not exist.
Arrays
It is used for the content format of commands sent by the client to the server, and also for the content format of commands sent by the server, such as: LRANGE
Format: “*”, followed by an integer to indicate the array length, then \r\n, followed by each element in the array, which can be of any RESP type (Intergers, Bulk strings, etc., above), separated by \r\n, ending with \r\n
Let’s see how the set name Foo command is passed from the client to the server
127.0.0.1:6379> set name Foo
OK
#The complete content sent by the client"*3\r\n$3\r\nset\r\n$4\r\nname\r\n$3\r\nFoo\r\n"
Copy the code
* 3
It means that this content is of type Arrays and length 3, becauseset name Foo
There are three strings\r\n
separator$3
The first string has length 3\r\n
separatorset
Content set\r\n
separator$4
The second string has length 4name
Specific Content Name\r\n
separator$3
The third string is of length 3\r\n
separatorFoo
Content Foo\r\n
At the end
Let’s look at what the server returns after the mget name name1 command is issued
127.0.0.1:6379> mget name name2
1) "Foo"
2) (nil)
#The server returns completion: *2\r\n$3\r\nFoo\r\n$-1\r\n"
Copy the code
* 2
That means that the content is of type Arrays and of length 2, because we’re looking up two keysname
andname2
\r\n
$3
The first string has length 3\r\n
Foo
The specific content Foo, which is just nowset name Foo
Effect of the command\r\n
The $1
The second string has length -1, which means nil, does not exist\r\n
Null array representation
127.0.0.1:6379> blpop foo 1
(nil)
#The server returns the completed content:"*-1\r\n"
Copy the code
Similarly, the client library must distinguish between null and empty arrays.
conclusion
Five scenarios for using RESP data