当前位置:网站首页>Can you use redis? Then come and learn about redis protocol
Can you use redis? Then come and learn about redis protocol
2022-07-26 21:49:00 【YYniannian】
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 .
边栏推荐
猜你喜欢

技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?

Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated

What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!

内容管理工具,用蓝色书签就足够

Devops has been practiced for many years. What is the most painful thing?

测试员:“我有五年测试经验”HR: “不,你只是把一年的工作经验用了五年”

【Flutter -- GetX】弹框 - Dialog、Snackbar、BottomSheet

月薪5万的朋友告诉我,你只是在打杂

从手动测试,到自动化测试老司机,只用了几个月,我的薪资翻了一倍

6、 Wechat applet release process
随机推荐
It is said that Samsung obtained EUV photoresist from Belgium
在上传之前预览图像
七、微信小程序运行报错:Error: AppID 不合法,invalid appid
Devsecops, speed and security
Industrial basic IFC - extract model structure tree
5、 Applet error: message:error: system error, error code: 80058, desc of scope userLocation is empty
Vi和Vim文本编辑器
Can I view the history in the "stealth" mode of the secure browser?
成功上岸了自动化测试岗,最高月薪15.4K,自己真棒~
Uncover the secrets of Xiaomi 100million pixel camera: 1/1.3 inch COMS sensor, resolution 12032 × nine thousand and twenty-four
按关键字搜索易贝商品 API
word-break: break-all VS word-wrap: break-word
Huawei released the top ten trends in 2025: 5g, robot, AI, etc
Flask对token的解码&挂载&装饰器&七牛云上传
Supplement - nonlinear programming
内容管理工具,用蓝色书签就足够
Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?
JDBC总结
Ren Zhengfei talked about the suppression of the United States again: to live is to win, and to defeat the United States
(C language) a brief introduction to define