Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of RESP protocol in Redis

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the role of the RESP protocol in Redis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Brief introduction

The client and server of Redis adopt a separate protocol called RESP (REdis Serialization Protocol). The author mainly considers the following points:

Easy to implement

Fast parsing

Human readable

Note: although RESP is designed for Redis, it can also be used in other Cripple S software.

Data types and examples

RESP can serialize the following types: integers, single-line replies (simple strings), arrays, error messages, and multiline strings. What the Redis client sends to the server is a string array of executed commands, and the server replies to different types of data according to different commands, but each part of the protocol ends with "\ r\ n" (CRLF). In addition, RESP is binary secure and does not need to handle the transfer from one process to another, because it uses the prefix length for transmission.

In RESP, some data types are determined by its first byte:

Single-line reply: the first byte of the reply is "+"

Error message: the first byte of the reply is "-"

Shaping number: the first byte of the reply is ":"

Multiline string: the first byte of the reply is "$"

Array: the first byte of the reply is "*"

Single-line reply

A string that begins with "+" and ends with "\ r\ n". E.g.

+ OK\ r\ n

The response client library should return something other than "+" and CRLF, such as "OK" for the above content. E.g.

127.0.0.1 redis-cli 6379 > set name TaoBeier+OK\ r\ n # the server actually returns-OK # server client displays an error message

The error message is very similar to a single-line reply, except that the "+" is replaced by "-". The real difference between the two is that the error message is treated as an exception by the client, and it is the error message itself that makes up the error type. E.g.

-Error message\ r\ n

Error messages are sent only when errors occur, such as data type errors, syntax errors, or commands that do not exist. When an error message is received, the client library should throw an exception. E.g.

127.0.0.1 ERR unknown command 6379 > TaoBeier-ERR unknown command 'TaoBeier'\ r\ n # server actually returns, the following is the same as-- (error) ERR unknown command' TaoBeier' # redis-cli client, and the following is the same as 127.0.0.1 ERR unknown command 6379 > set name TaoBeier moelove-ERR syntax error\ r\ nMurray-(error) server integer

This type simply uses a string prefixed with ":" and ending with CRLF to represent integers. E.g. ": 666\ r\ n" or ": 999\ r\ n" are integer replies. Many commands return integer replies, such as commands such as INCR LLEN LPUSH. However, in most cases, the integer reply returned does not have much practical meaning. For example, LPUSH is just to indicate that several values have been inserted, but for example, the EXISTS command returns 1 and false when the result is true. E.g.

127.0.0.1 redis-cli 6379 > LPUSH info TaoBeier MoeLove:2\ r\ n # the server actually returns, as shown below-(integer) 2 # server The following is the same as 127.0.0.1 EXISTS info:1 6379 > LLEN info:2\ r\ nMurray-(integer) 2127.0.0.1 EXISTS info:1 > EXISTS info:1\ r\ nMurray-(integer) 1127.0.0.1 EXISTS info:1 6379 > DEL info:1\ r\ nMuhashi-(integer) 1127.0.1 EXISTS info:1 6379 > EXISTS info:0\ r\ nMutual-(integer) 0 multiline string

Multiline strings are used by the server to return a single binary secure string with a maximum length of 512MB. It starts with "$", followed by the actual number of bytes to be sent, followed by CRLF, then the actual string data, and ends with CRLF. So, for example, if we want to send a string of "moelove.info", it is actually encoded as "$12\ r\ nmoelove.info\ r\ n". If an empty string is sent, it will be encoded as "$0\ r\ n\ r\ n". In some cases, when you want to represent a value that does not exist, it returns "$- 1\ r\ n", which is called an empty multiline string, and when the client library receives this response, it should also return a null value (for example, nil) instead of an empty string. E.g.

127.0.0.1 redis-cli 6379 > set site moelove.info+OK\ r\ n # the server actually returns, as shown below-OK # server Same as 127.0.0.1 nmoelove.info 6379 > get site$12\ r\ nmoelove.info\ nMurray-"moelove.info" 127.0.0.1 moelove.info 6379 > del site:1\ r\ nMurray-(integer) 1127.0.1 moelove.info 6379 > get site$-1\ r\ nMuzai-(nil) 127.0.0.1luv 6379 > set site'+ OK\ r\ n---OK127.0.0.1:6379 > get site$0\ r\ n\ r\ nMuyashi-"array"

The array type can be used by the client to send commands to the server. Similarly, when some commands combine elements to return to the client, they also use the array type as the reply type. It starts with "*", followed by the number of elements returned, followed by CRLF, and then the type of each element in the array. The most typical is the LRRANGE command, which returns elements from a list. E.g.

127.0.0.1 redis-cli 6379 > LPUSH info TaoBeier moelove.info:2\ r\ n # the server actually returns, as shown below-(integer) 2 # server The following is the same as 127.0.0.1moelove.info 6379 > LRANGE info 0-1mm 2\ r\ nhorse 12\ r\ nmoelove.info\ r\ nluk 8\ r\ nTaoBeier\ r\ nMuhashi 1) "moelove.info" 2) "TaoBeier" 127.0.1TaoBeier 6379 > LPOP info$12\ r\ nmoelove.info\ r\ nRue-"moelove.info" 127.0.0.1Rose 6379 > LPOP info$8\ r\ nTaoBeier\ r\ nUV-"TaoBeier" 127.0.0.1: 6379 > is it helpful for you to read the above content after reading 6379 > LRANGE info 0-1? 0\ r\ nMurray-(empty list or set)? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report