Redis Communication Protocol Specification

Source: Internet
Author: User

Redis Communication Protocol Specification

Redis is a key-value-based memory-based database. Program Then we know that we can use redis-CLI to connect to redis-server for communication and perform various operations. So how do we use advanced languages such as C, Java, Python, and C # To send a request to operate data and get the relevant data from the database (replies? If you have these questions Article Suitable for you to continue to see PS: the official website has recommended a variety of language client implementation program http://redis.io/clients interested can be analyzed Source code
How to send commands? Socket! Through the socket in the form of TCP protocol, we can connect to the redis-server, and then send some Specific format Command and related data. All commands end with \ r \ n (cr lf)
Special Command? The special format mentioned above is the so-called redis communication protocol, which ensures the specification of the format of the sent command, data, and returned data. In this way, we can correctly send commands to the redis-server, and then parse the returned data (reply data) according to the protocol format to get the desired form.
Protocol specifications: Standard protocol: this protocol has been introduced in redis1.2, but it is only used as a standard way to communicate with redis-server in version 2.0. In this new protocol, all parameters sent to the redis-server are binary safe (to ensure binary security). The basic format is as follows: * <number of parameters> cr lf $ <number of 1 bytes> cr lf <parameter 1> Cr lf... $ <parameter n Bytes> cr lf <parameter n> CR LF
For example, the form corresponding to set mykey myvalue should be as follows, * 3 cr lf // three parameters $3 cr lf // The first parameter set has three bytes set cr lf // parameter content set $5 cr lf // The second parameter mykey has five bytes mykey cr lf // parameter 2 content mykey $7 cr lf // The third parameter has seven bytes myvalue cr lf // parameter 3 content myvalue
 

The result in string format is:"* 3 \ r \ N $3 \ r \ NSET \ r \ N $5 \ r \ nmykey \ r \ N $7 \ r \ nmyvalue \ r \ n"
 

In fact, the returned data also complies with the Protocol format. For example, $6 \ r \ nmydata \ r \ n is a data returned from redis-server, called block response data, usually a string (there are several other types, later ). In addition, redis can also return a data list, which can return multiple pieces of data (called multiple pieces of data ). The returned results always start with "* Number of parameters \ r \ n". The number of parameters indicates a total of several pieces of data (which can be understood as returning multiple strings)

Response type: Determining the type of a response is determined by the first byte of the returned data. There are several types: "+" Indicates a status information such as + OK "-" Indicates that an error has been sent (for example, the type of operation error) ":" Return an integer, for example, ": 11 \ r \ n. Some commands return integers without any meaning. For example, lastsave returns an integer of the timestamp, and incr returns a value after 1; some commands, such as exists, will return 0 or 1, indicating whether to be true or false; others, such as sadd, Srem returns 1 when the operation is actually executed; otherwise, return 0 "$" Returns a piece of data that is used to return a binary secure string, such as get mykey: "$6 \ r \ nsongzh \ r \ n "; if the requested value does not exist, $-1 is returned. When implementing the client program, you should actually prompt that the user value does not exist and return null "*" Returns multiple pieces of data (used to return multiple values ). , The first byte is always "*", followed by the number of corresponding values, such: C: lrange mylist 0 3 S: * 4 S: $3 S: foo S: $3 S: bar S: $5 $: World If the specified value does not exist, * 0 is returned.
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.