Redis client protocol parsing, redisprotocol

Source: Internet
Author: User
Tags redis cluster

Redis client protocol parsing, redisprotocol

On the official website, visit http://redis.io/topics/protocolto describe the rediscommunication agreement.
For the following reasons, I want to parse the redis client protocol:

1. Be familiar with communication protocols to facilitate better system design.

2. Learning the design concept of RESP not only expands my thinking, but may be applied to my code in the future.

3. Some people want to directly integrate the redis client into their existing systems, including me. This will be explained in my next article.

Let me translate the following: http://redis.io/topics/protocolsome of which I think are important:

Redis clients communicate with the Redis server using a protocol calledRESP(REdis Serialization Protocol). While the protocol was designed specifically for Redis, it can be used for other client-server software projects.

RESP is a compromise between the following things:

  • Simple to implement.
  • Fast to parse.
  • Human readable.

RESP can serialize different data types like integers, strings, arrays. there is also a specific type for errors. requests are sent from the client to the Redis server as arrays of strings representing the arguments of the command to execute. redis replies with a command-specific data type.

RESP is binary-safe and does not require processing of bulk data transferred from one process to another, because it uses prefixed-length to transfer bulk data.

Note: the protocol outlined here is only used for client-server communication. Redis Cluster uses a different binary protocol in order to exchange messages between nodes.

Translation:

The redis client communicates with the redis server through a Protocol called RESP (REdis Serialization Protocol. Although this protocol is specifically designed for redis, it can be used in other software projects based on the C/S model.

RESP is a compromise based on the following facts:

  • Easy to implement
  • Quick explanation
  • Human readable

RESP can serialize different data types, such as integer, String, array, and special error types. The client sends a string array request to the server, while the string array indicates that the command parameters are executed. Redis will reply with a dedicated Command type.

RESP is binary secure and does not require a large amount of data processing during the process conversion because it uses the prefix length to convert batch data.

Note: The Protocol outlined here is only used for client-server communication. The Redis cluster uses a different binary protocol for message exchange between different nodes.

RESP is actually a serialization protocol that supports the following data types: Simple Strings, Errors, Integers, Bulk Strings and Arrays.

The way RESP is used in Redis as a request-response protocol is the following:

  • Clients send commands to a Redis server as a RESP Array of Bulk Strings.
  • The server replies with one of the RESP types according to the command implementation.

In RESP, the type of some data depends on the first byte:

  • ForSimple StringsThe first byte of the reply is "+"
  • ForErrorsThe first byte of the reply is "-"
  • ForIntegersThe first byte of the reply is ":"
  • ForBulk StringsThe first byte of the reply is "$"
  • ForArraysThe first byte of the reply is"*"

Additionally RESP is able to represent a Null value using a special variation of Bulk Strings or Array as specified later.

In RESP different parts of the protocol are always terminated with "\ r \ n" (CRLF ).

Translation:

RESP is actually a serialization protocol that supports the following data types: short strings, errors, integers, long strings, and arrays.

As a request-response protocol, RESP is used in Redis as follows:

  • The client sends a command like a RESP long string array to the Redis server.
  • The Redis server replies to one of the RESP types based on commands.

In RESP, a data type is based on the first byte:

  • For short strings, the first byte to be replied is "+"
  • For errors, the first byte of the reply is "-"
  • For integers, the first byte returned is ":"
  • For long strings, the first byte to be replied is "$"
  • For arrays, the first byte to be replied is "*"
In addition, the RESP can use a specified long string or a special variable of an array to indicate a null value.
In RESP, different parts of the Protocol always end with "\ r \ n" (CRLF.

The protocol mentioned above emphasizes the client, that is, the protocol used by the server to reply to the client request. When the client requests the server, it only needs to add "\ r \ n" after the command ".

There are five types of returned instances:

Assume that the following key-value pairs exist in the redis server: name1catage110 short string "set name2 fish \ r \ n" "+ OK \ r \ n" error "seet name3 dog" "-ERR unknown command 'seet '\ r \ n" integer "incr age1 "": 11 "long string ①" get name1 \ r \ n "" $3 \ r \ ncat \ r \ n "②" get name3 \ r \ n "" $-1 \ r \ n "array ①" mget name1 age1 \ r \ n "" * 2 \ r \ n $3 \ r \ ncat \ r \ n $2 \ r \ n11 \ r \ n "②" mget name2 age2 \ r \ n "" * 2 \ r \ n $4 \ r \ nfish \ r \ n $-1 \ r \ n "③ other cases "*-1 \ r \ n" and "* 0 \ r \ n" are returned ", for details, refer to the official redis documentation;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.