当前位置:网站首页>Can you use redis? Then come and learn about redis protocol
Can you use redis? Then come and learn about redis protocol
2022-07-18 07:36:00 【Fat technology house】

brief introduction
redis Is a very good software , It can be used as an in memory database or as a cache . Because of his excellent performance ,redis It is used in many occasions .
redis Is a client-side and server-side model , The client and server are through TCP Protocol to connect , The client sends the request data to the server , The server returns the request to the client . Such a request process is completed .
Of course, in the beginning , Because very few people use it , The system is not stable enough , adopt TCP The data transmitted by the protocol is not standardized . But more and more people are using it , In particular, it is hoped to develop software that is suitable for different languages and platforms redis Client time , It is necessary to consider the issue of compatibility .
At this time, the client and server need a unified interaction protocol , about redis For example, this general interaction protocol is called Redis serialization protocol(RESP).
RESP Is in Redis 1.2 Version of , And in Redis 2.0 And Redis Standard way of server communication .
That is to say , from Redis 2.0 after , It can be based on redis protocol The protocol developed its own redis The client .
redis Advanced usage of
Generally speaking ,redis The client side and the server side of the consist of a request - Patterns of response , That is, the client sends a request to the server , Then get the response result from the server .
The request and response are redis The simplest use of . be familiar with redis My friend may think of two redis Advanced usage of , These two usages are not traditional requests - Response patterns .
What are the two usages ?
The first is redis Support pipline, That is, pipeline operation , The advantage of the pipeline is redis The client can send multiple commands to the server at one time , Then wait for the server to return .
The second kind redis And support Pub/Sub, The broadcast model , In this case , It is not a request and response pattern , stay Pub/Sub Next , Switch to the server-side push mode .
Redis Medium pipline
Why use pipline Well ?
because redis Is a typical request response pattern , Let's take a common example incr An example of an order :
Client: INCR X Server: 1 Client: INCR X Server: 2 Client: INCR X Server: 3 Client: INCR X Server: 4
In fact, the client just wants to get the final result , But every time the client needs to wait for the server to return the results , To send the next command . This will lead to a problem called RTT(Round Trip Time) A waste of time .
Although every time RTT It's not long , But it is also a very objective figure .
Can I send all the client commands to the server together ? This optimization is called Pipeline.
piepline This means that the client can continue to send commands to the server without receiving a return from the server .
The above command can be used pipline Rewrite as follows :
(printf "INCR X\r\nINCR X\r\nINCR X\r\nINCR X\r\n"; sleep 1) | nc localhost 6379
:1
:2
:3
:4
Copy code because redis Server support TCP Protocol to connect , So we can use nc Connected to the redis Execute the command in the server .
In the use of pipline There is one thing to pay attention to , because redis The server caches the requested results on the server side , wait until pipline After all the commands in the are executed, they will be returned uniformly , So if the server returns more data , Need to consider the problem of memory occupation .
that pipline Just to reduce RTT Do you ?
Friends who are familiar with the operating system may have heard of the concept of user space and operating system space , Read data from user input and then write it to system space , This involves a user space switch , stay IO In operation , Such space switching or copying is time-consuming , If you request and respond frequently , This will cause frequent space switching , Thus, the efficiency of the system is reduced .
Use pipline You can send multiple instructions at once , So as to effectively avoid space switching behavior .
Redis Medium Pub/Sub
and Pub/Sub The relevant order is SUBSCRIBE, UNSUBSCRIBE and PUBLISH.
Why use Pub/Sub Well ? Its main purpose is decoupling , stay Pub/Sub The sender does not need to know the address of the receiver , Similarly, for the message receiver , There is no need to know the address of the specific message sender . They just need to know the associated topic .
subscribe and publish The command of is relatively simple , Let's give you an example , First, the client subscribe topic:
redis-cli -h 127.0.0.1
127.0.0.1:6379> subscribe topic
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "topic"
3) (integer) 1
Copy code Then at another terminal , call publish command :
redis-cli -h 127.0.0.1
127.0.0.1:6379> publish topic "what is your name?"
(integer) 1
Copy code You can see that the client will receive the following message :
1) "message"
2) "topic"
3) "what is your name?"
Copy code RESP protocol
RESP The agreement has 5 Types , Namely imple Strings, Errors, Integers, Bulk Strings and Arrays.
Different types take the first in the message byte Distinguish , As shown below :
| type | first byte |
|---|---|
| Simple Strings | + |
| Errors | - |
| Integers | : |
| Bulk Strings | $ |
| Arrays | * |
protocol The different parts of the are marked with "\r\n" (CRLF) To make a difference .
Simple Strings
Simple Strings Is a simple string .
Usually used in server-side returns , The format of this message is "+" Plus text messages , Finally "\r\n" ending .
For example, the server returns OK, Then the corresponding message is :
"+OK\r\n"
Copy code The above message is a non binary secure message , If you want to send binary secure messages , You can use Bulk Strings.
What is a non binary secure message ? about Simple Strings Come on , Because the message is "\r\n" ending , So the message cannot contain "\r\n" These two special characters , Otherwise, it will have the wrong meaning .
Bulk Strings
Bulk Strings It's binary safe . This is because Bulk Strings Contains a character length field , Because the character length is judged according to the length , Therefore, there is no disadvantage of judging whether the character ends according to a specific character in the character .
To be specific Bulk Strings The structure of is "$"+ String length +"\r\n"+ character string +"\r\n".
With OK For example , If the Bulk Strings To express , As follows :
"$2\r\nok\r\n"
Copy code Bulk Strings It can also contain empty strings :
"$0\r\n\r\n"
Copy code Of course, it can also mean nonexistent Null value :
"$-1\r\n"
Copy code RESP Integers
This is a redis An integer in represents , The specific format is ":"+ Integers +"\r\n".
such as 18 This integer can be expressed in the following format :
":18\r\n"
Copy code RESP Arrays
redis Multiple commands of can be in the form of array To express , Multiple values returned by the server can also be used arrays To express .
RESP Arrays The format is "*"+ Number of elements in the array + Other similar data .
therefore RESP Arrays Is a composite structure of data . For example, an array contains two Bulk Strings:"redis","server" You can use the following format to represent :
"*2\r\n$5\r\nredis\r\n$6\r\nserver\r\n"
Copy code RESP Arrays Primitives in can not only use different types , It can also contain RESP Arrays, That is to say array Nesting of :
"*3\r\n$5\r\nredis\r\n$6\r\nserver\r\n*1\r\n$4\r\ngood\r\n"
Copy code To facilitate observation , Let's format the above message :
"*3\r\n
$5\r\nredis\r\n
$6\r\nserver\r\n
*1\r\n
$4\r\ngood\r\n"
Copy code The above message is an array of three elements , The first two elements are Bulk Strings, The last one is an array containing one element .
RESP Errors
Last ,RESP It can also represent an error message .RESP Errors The message format of is "-"+ character string , As shown below :
"-Err something wrong\r\n"
Copy code In general ,"-" The following first word indicates the error type , But this is just a conventional rule , Not at all RESP Mandatory requirements in the agreement .
in addition , After comparison , You may find that RESP Errors and Simple Strings Yes, the message format is similar .
This processing of different message types is differentiated on the client side .
Inline commands
If you press RESP Requirements of the agreement , When we connect to the server, we need to include RESP Define all the formats of messages in , But these messages contain additional message types and carriage return line breaks , So it would be confusing to use the protocol directly .
therefore redis Some inline commands are also provided , That is, a condensed version of the protocol command , This compact version removes message types and carriage return line breaks .
We use "get world" For example, this order . Let's take a look at the connections in different ways .
The first is to use redis-cli Connect :
redis-cli -h 127.0.0.1
127.0.0.1:6379> get world
"hello"
Copy code because redis-cli yes redis The client of , So you can use it directly inline command To execute an order .
If you use telnet, We can also use the same command to get the results :
telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get world
$5
hello
Copy code You can see that the return result is "$5\r\nhello\r\n".
If you want to use protocol messages to request redis What should the server do ?
The order we want to ask is "get world", Turn it into RESP The message for is :
"*2\r\n$3\r\nget\r\n$5\r\nworld\r\n"
Copy code Let's try using the above command nc Pass on to redis server On :
(printf "*2\r\n$3\r\nget\r\n$5\r\nworld\r\n"; sleep 1) | nc localhost 6379
-ERR Protocol error: expected '$', got ' '
Copy code I'm sorry we got ERR, Can't you use it directly RESP Message format for transmission ? Of course not. , The problem is $ A symbol is a special character , We need to escape :
(printf "*2\r\n\$3\r\nget\r\n\$5\r\nworld\r\n"; sleep 1) | nc localhost 6379
$5
hello
Copy code You can see the output and use it directly redis-cli Agreement .
summary
That's all RESP Basic contents of the protocol and examples of manual use , With RESP, We can then create the..., according to the format defined in the protocol redis client .
Maybe you will ask again , Why is it just redis The client ? Is there an agreement redis The server side can also be created ? The answer, of course, is yes , You only need to transmit messages according to the protocol . The main problem is redis The implementation of the server side is complex , It's not that easy .

边栏推荐
- How to migrate the complex SQL statements used by applications developed for Oracle to polardb for POS
- 客户端那些事儿(2)
- 【示波器的基本使用】以及【示波器按键面板上各个按键含义的介绍】
- 头文件里面的ifndef /define/endif的作用
- 渗透测试流程
- ab网站压力测试
- MySQL 为什么临时表可以重名 ?
- 正则表达式保留【匹配范围内的内容】,然后替换【匹配范围之外的内容】
- LAN attack and network device security configuration
- 来自JRockit的礼物:JMC虚拟机诊断工具
猜你喜欢

Illegal profits exceed one million, and new outlets in the industry are being cracked and eroded

实现浏览器 - Servlet - 数据库交互操作

Gift from JRockit: JMC virtual machine diagnostic tool

VxWorks环境搭建与学习

ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the

How to greedy match in VIM

Getting started with OpenCV ----- vs how to install opencv Library

记录Yolov5的使用(1)

非法获利超百万,行业新风口正被破解侵蚀

Eight guidelines for modbus-rs485 wiring
随机推荐
请问通达信如何开户?请问手机开户股票开户安全吗?
Openharmony module II parsing of header files under interfaces (8)
(Dell Lingyue 7572) after the laptop expands the display, the laptop has no sound
模块二interfaces下头文件解析(3)
Using the array of C to realize the addition, subtraction, multiplication and transpose of matrix
OpenHarmony模块二下文件samgr_server解析(5)
时间复杂度和空间复杂度详解
Openharmony related knowledge learning
C语言:【位域操作】(结构体中使用冒号)
Meituan's one-sided experience and detailed answers
OpenHarmony模块二初分析(2)
Introduction to C language (6)
CAS与AQS简单理解
C#小技巧 获取枚举所有枚举值
现在网上开户安全么?想知道国内十大劵商是哪些?
(尚硅谷)JDBC总复习
Preliminary analysis of openharmony module II (2)
The regular expression retains [content within the matching range], and then replaces [content outside the matching range]
How to use hhdbcs to debug polardb for PostgreSQL stored procedures?
BigDecimal比较大小