preface

Good brothers, from the previous chapterI guarantee you haven’t used Redis GEOAfter the end of the whole Redis seriesThe data structure(Some are not data structures) and that’s it. From the first five basic data types to the following imageBitmaps,HyperLogLog,PipelineWait, have not read the good brother can turn over my article, remember to like and follow yo (manual comparison heart). The last stage covered some advanced features in general, so the next part will be about the client side. Such as communication protocols,Jedis,RedisTemplateWait, good brothers, wait and see.

An overview of the

Communication protocol

Communication protocol refers to the rules and conventions that two entities must follow to complete communication or services. Data communication systems in different geographical locations interconnected by communication channels and devices must have a common language to make them work together to realize information exchange and resource sharing. What to communicate, how to communicate, and when to communicate must follow certain mutually acceptable rules. This rule is the communication protocol.

Redis uses protocols

  1. Connection:In Redis, the communication protocol between client and server is inTCPBuilt on top of the protocol.
  2. Data format:Redis developedRESP(REdis Serialization Protocol) realizes the normal interaction between the client and the server. This Protocol is simple and efficient, and can be analyzed by machines and easily recognized by humans.

RESP agreement

The protocol was designed specifically for Redis to serialize different data types, such as integers, strings, and Arrays. It also uses a special type for errors. The request is sent from the client to the Redis server as an array of strings representing the parameters of the command to execute. Redis uses command-specific data types as replies. The RESP protocol is binary safe and does not need to deal with the size of the block data transferred from one process to another because it uses prefixed-length to transfer the block data. Note that this protocol is only used for client-server communication. The Redis cluster uses different binary protocols to exchange messages between nodes.

Send command Format

The RESP specifies the format of a command, CRLF stands for \r\n.

*< number of parameters > CRL$The < parameter1Number of bytes > CRLF < parameter1> CRLF
...
$< number of bytes of parameter N> CRLF < parameter N> CRLFCopy the code

Take the set hello world command as an example

## The number of arguments is 3, so the first behavior:
*3
The ## arguments are 355 bytes, so the following actions:
$3
SET
A $5
hello
A $5
world
Copy the code

It should be noted that the above is just a formatted display of the results, the actual transmission format is the following code

*3\r\n$3\r\nSET\r\nA $5\r\nhello\r\nA $5\r\nworld\r\n
Copy the code

Response result format

Redis returns five types of results:

  1. Status reply: YesRESPThe first byte is+.
  2. Error reply: YesRESPThe first byte is-.
  3. Integer reply: yesRESPThe first byte is:.
  4. String reply: yesRESPThe first byte is$.
  5. Multiple string replies: yesRESPThe first byte is*.

In redis-cli, you can only see the final result (e.g., set hello world, the result is OK, but you can’t see the result similar to +), that is because redis-cli itself is based on RESP, so you can’t see the intermediate result. Redis-cli. c source code to the command result parsing structure is as follows:

static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
sds out = sdsempty();
switch (r->type) {
case REDIS_REPLY_ERROR:
// Handle the error reply
case REDIS_REPLY_STATUS:
// Process the status return
case REDIS_REPLY_INTEGER:
// Process integer replies
case REDIS_REPLY_STRING:
// Handle string replies
case REDIS_REPLY_NIL:
/ / null
case REDIS_REPLY_ARRAY:
// Handle multiple string replies
return out;
}
Copy the code

How do I see the “real” results from the Redis server? You can use nc commands, Telnet commands, or even write a socket program to simulate. Take the NC command for example

## Connect to Redis
nc 127.0.0.1 6379
Run the set command
set hello world
The status reply is returned
+OK
Execute a command that does not exist
sethx
## Error reply
-ERR unknown command 'sethx'
Perform the addition operation
incr counter
## integer reply
:1
## Perform the value operation
get hello
## string reply
A $5
world
## Set multiple key-value pairs in batches
mset java jedis python redis-py
## Batch get mGET
mget java python
## Return the number of results
*2
## string reply
A $5
jedis
$8
redis-py
If the key does not exist, return $-1
get not_exist
## If there is a nil value, $-1 will be returned whether it is a string reply or multiple string replies.
$- 1
Copy the code

conclusion

Pay attention to the point

Since the protocols described above are request-response based, the following function points need to be excluded:

  1. Redis support Pipeline operation, not familiar with the good brothers can look at Redis Pipeline this article is enough (the whole series of basic foundations have). So customers can send multiple commands at once and wait for a response later.
  2. When the Redis client subscribesPub/SubThe protocol changes the semantics to a push protocol, that is, the client no longer needs to send a command, because the server automatically sends the new message to the client as soon as it receives the message (for clients subscribed to the channel).
  3. The Redis cluster uses a different binary protocol to exchange messages between nodes, not this protocol.

The official documentation

Redis protocol specification

twitter

For Redis communication protocol, in fact, the most important thing we need to know is how the client connects to the server, and the format of the message sent and responded to. In depth is left to good brothers to explore, after reading this series we are not a will onlyThe get and setHe’s a good brother. Redis series to get this actually has a good number of usually we have not used things, continue to come on!!

That’s the end of this issue. Welcome to leave your comments in the comments sectionAsk for attention, ask for likes

I’m sure you haven’t used Redis GEO