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

How to understand Redis communication protocol RESP

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

Share

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

This article shows you how to understand the Redis communication protocol RESP, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The author of Redis believes that the bottleneck of the database system generally does not lie in the network traffic, but in the internal logic processing of the database itself. So even if Redis uses a text protocol that wastes traffic, it can still achieve extremely high access performance. Redis puts all the data in memory and uses a single thread to provide services. A single node can achieve the ultra-high QPS of 10w/s when running full of a CPU core.

RESP (Re dis S erialization P rotocol)

RESP is short for Redis serialization protocol. It is an intuitive text protocol, which has the advantages of simple implementation and excellent parsing performance. The Redis protocol divides the transmitted structural data into five minimum unit types, and the unit ends uniformly with the carriage return newline symbol\ r\ n.

A single-line string begins with a + symbol.

The multiline string begins with the $symbol, followed by the string length.

The integer value begins with the: symbol, followed by a string of integers.

The error message begins with the-symbol.

The array begins with a * sign, followed by the length of the array.

Single-line string

# one-line string hello world+hello world\ r\ n

Multiline string

# Multiline string hello world. Of course, a multiline string can also represent a single-line string. $11\ r\ nhello world\ r\ n

Integer number

# Integer 1024 1024\ r\ n

Error messa

# error parameter type error-WRONGTYPE Operation against a key holding the wrong kind of value

Array

# Array [1Permin2Pol 3] * 3\ r\ nVO1\ r\ nVOL2\ r\ nR3\ r\ n

Note:

NULL: expressed as a multi-line string, but the length should be written as-1.

$- 1\ r\ n

Empty string: represented by a multi-line string with a length of 0.

$0\ r\ n\ r\ n

Notice that there are two\ r\ n. Why two? Because the two\ r\ nare separated by empty strings.

Client request instruction

The instructions sent by the client to the server are in only one format, an array of multiline strings.

For example:

> set name mango# instruction conversion * 3\ r\ nroom3\ r\ nset\ r\ ndirective 4\ r\ name\ r\ ndirective 5\ r\ mango\ r\ n# output this string * 3 $3set$4name$5mango on the console

Server response result

The response from the server to the client supports a variety of data structures, so the message response is structurally complex. However, the most complex response message is a combination of the above five basic types.

Single-line string response

> set name mangoOK# response message + OK

Error response

> incr name (error) ERR value is not an integer or out of range# response message-ERR value is not an integer or out of range

Integer type response

> incr number (integer) response message: 1

Multiline string response

> get name "mango" # response message the string enclosed in double quotes here is the multiline string response $5mango

Array response

> hset user name mango (integer) 1 > hset user age 18 (integer) 1 > hgetall user1) "name" 2) "mango" 3) "age" 4) "18" # response message * 4 $4name$5mango$3age$218

Nesting

> scan 0 match name* count 101) "0" 2) 1) "name1" 2) "name2" # response message * 2 $109.2 $5name1 $5name2

There are a large number of redundant carriage return newlines in the Redis protocol, but this does not prevent it from becoming a very popular text protocol in the field of Internet technology. There are many open source projects that use RESP as its communication protocol. In the field of technology, performance is not always everything, but also simplicity, understandability, and implementability, which need to be properly weighed.

The above content is how to understand the Redis communication protocol RESP. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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: 249

*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