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

Introduction of redis Communication Protocol protocol

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Ping pong of redis

After logging in to the redis cli client, enter ping, and the server will return pong to indicate that the connection is intact and that the server is generally functioning normally.

The first line is the client I started with docker. If you are not docker, you can start redis-cli normally.

After ping, you will receive pong.

Using Java socket to implement ping pong of Redis

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.210", 6379); / / oi stream OutputStream os = socket.getOutputStream (); InputStream is = socket.getInputStream (); / / write os.write to the redis server ("PING\ r\ n" .getBytes ()) / / read from redis server to bytes byte [] bytes = new byte [1024]; int len = is.read (bytes); / / to string output System.out.println (new String (bytes,0,len));}

Why is there a'+ 'symbol? There is no such plus sign in redis-cli?

This is related to the communication protocol. We will introduce the specific meaning later. But redis-cli just swallowed the'+ 'symbol and didn't show it.

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.210", 6379); / / oi stream OutputStream os = socket.getOutputStream (); InputStream is = socket.getInputStream (); / / write os.write to the redis server ("PING\ r\ n" .getBytes ()) / / read from redis server to bytes byte [] bytes = new byte [1024]; if (is.read () = ='+') {/ / to string output int len = is.read (bytes); System.out.println (new String (bytes,0,len)) } / / else if $/ / else if * / / else}

Implement SET and GET

Set:

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.210", 6379); / / oi stream OutputStream os = socket.getOutputStream (); InputStream is = socket.getInputStream (); / / write os.write to the redis server ("set hello world123\ r\ n" .getBytes ()) / / read from redis server to bytes byte [] bytes = new byte [1024]; int len = is.read (bytes); / / to string output System.out.println (new String (bytes,0,len));}

Get:

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.310", 6379); / / oi stream OutputStream os = socket.getOutputStream (); InputStream is = socket.getInputStream (); / / write os.write to the redis server ("get hello\ r\ n" .getBytes ()) / / read from redis server to bytes byte [] bytes = new byte [1024]; int len = is.read (bytes); / / to string output System.out.println (new String (bytes,0,len));} explain the + and $symbols in the above example

The plus sign'+ 'indicates the status reply. When the redis server returns status information to the client, it will send a `+` symbol to start with.

Then there is the corresponding status information, such as' OK''or something.

Finally, it ends with'\ r\ n'. Let's take a look at the code and see.

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.210", 6379); / / oi stream OutputStream os = socket.getOutputStream (); InputStream is = socket.getInputStream (); / / write os.write to the redis server ("set hello world123\ r\ n" .getBytes ()) / / read from the redis server to bytes byte [] bytes = new byte [1024]; if (is.read () = ='+') {System.out.println ("this is a status reply! How do you know? `+` means "status reply"); int len = is.read (bytes); System.out.println ("status of reply is:" + new String (bytes, 0, len));} / / do you want to see how many characters there are in bytes? System.out.println (Arrays.toString (bytes)); / / output is [79,75,13,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }

$indicates batch reading, and the general format is: $, the number of bytes of the content of the text

After grabbing the packet, the client sends "get hello" to the server, and the server sends the two blue lines to the client.

Public static void main (String [] args) throws Exception {/ / socket Socket socket = new Socket ("140.143.135.210", 6379); / / oi stream OutputStream os = socket.getOutputStream (); / / to parse'\ r\ n' for convenience, I changed the character stream BufferedReader br = new BufferedReader (new InputStreamReader (socket.getInputStream (). / / write os.write ("get hello\ r\ n" .getBytes ()) to the redis server; / / buffer array char [] chars = new char [1024]; / / read from the redis server to if (br.read () = ='$') {System.out.println ("this is a batch reply! How do you know? the `$ `sign means' batch reply'); System.out.println ("$is followed by a number to indicate the size of the text content"); / / readLine can directly judge'\ r'\ n' int len = Integer.parseInt (br.readLine ()) System.out.println ("$is followed by" + len + ", indicating that the text is" + len + "bytes, and then just read" + len + "bytes") / / then only read len characters to ok (in fact, the unit should be bytes, but I switched to the character stream in order to save trouble for readLine, the number is constant) br.read (chars, 0, len) System.out.println ("the result of get is:" + new String (chars, 0, len) + ", counting is really" + len + "characters");}}

Is that all about the Redis communication protocol?

No!!! The "get hello" just sent by the client to the server is only an "inline command", not the real communication protocol of Redis.

Q: what do you mean? A: that is to say, you can send it to the server as before. after the server receives it, it will go through the content you sent, and finally analyze the meaning of the content according to the space.

Q: what's wrong with that? A: if this is the case, you will leave the parsing to the server, which will increase the workload of the server.

Q: then how is it in line with the specification? Does it really improve the efficiency of the server if it complies with the agreement? A: first of all, take a look at the interaction between the client and the server in accordance with the protocol. Examples are as follows:

Example: set java python, after catching the package, it looks like this:

Red is the content sent by the client, and blue is the content returned by the server.

Let's analyze it together:

* 3 indicates that the client is about to send 3 pieces of content

Which three paragraphs? First paragraph:'$3 SET', second paragraph:'$4 java', third paragraph:'$6 python'

To put it more strictly: the first paragraph:'$3\ r\ nSET\ r\ n' the second paragraph:'$4\ r\ njava\ r\ n' the third paragraph:'$6\ r\ npython\ r\ n'

The meaning of the $symbol has been mentioned in the previous section, indicating the length of the following content, easy for the server to read.

For example: $6 has already reported the length of the python, and the server only needs to intercept the interval [index, index+6]. There is no need to find out where the space is (the time complexity of finding the space is O (n), while $6 is O (1)).

Jedis

In fact, what Jedis does is to convert a format like SET key value into the following format, and then send it to the Redis server:

* 3\ r\ nroom3\ r\ nSET\ r\ nroom3\ r\ nkey\ r\ nroom5\ r\ nvalue\ r\ n

These are the details of redis Communication Protocol (protocol), please pay attention to other related articles!

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

*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

Database

Wechat

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

12
Report