Learning redis source code [Command Protocol format]

Source: Internet
Author: User
Introduction

This article will introduce redis Command Parsing, but to have a more intuitive understanding of redis Command Parsing, you must first understand the redis Command Protocol format.

Source code

None (or in network. c)

Analyze requests

* <Number of arguments> CR LF
$ <Number of bytes of argument 1> CR LF
<Argument data> CR LF
...
$ <Number of bytes of argument n> CR LF
<Argument data> CR LF

Redis currently supports the following common binary secure request formats. Specific instances include:

* 3
$3
Set
$5
Mykey
$7
Myvalue

Convert to the actual form string of each byte as follows: "* 3 \ r \ N $3 \ r \ NSET \ r \ N $5 \ r \ nmykey \ r \ N $7 \ r \ nmyvalue \ r \ n ". The protocol in this format is also used by the redis server to reply to the client, called bulk reply.

This unified request protocol is also used in redis to send list items of some columns to clients, which is called multi bulk reply. It is composed of different bulk reply of N plus a * <argc> \ r \ n prefix, where argc is the number of bulk reply.

Replies

Redis will reply different types of replies to cient. Depending on the first byte, different types of reply replies can be determined:

  • Status reply, the first byte is "+"
  • Error reply, the first byte is "-"
  • Integer reply, the first byte is ":"
  • Bulk reply, the first byte is "$"
  • Multi bulk reply, the first byte is "*"
  • Status reply

+ <Status strings> \ r \ n

  • Error reply

-<Error type> <error info> \ r \ n

  • Integer reply

: <Integer value strings> \ r \ n

  • Bulk reply

$ <Number of bytes of The args> \ r \ n <ARGs strings> \ r \ n

  • Multi bulk reply

* <Number of bulk replies> \ r \ n <bulk reply list> \ r \ n

Description

In multi-bulk replies, if the length of an element is-1, the element is lost or null.

Multiple commands and pipelining

A client can use the same connection to send multiple commands. Pipeline technology allows you to use a single write operation to send multiple client commands without waiting for the server to reply after sending a command. The responses to these requests can be read in a unified manner at the end.

Inline commands

Redis also supports the early protocol format, that is, the form in the command line is as follows:

C: PINGS: +PONG
C: EXISTS somekeyS: :0






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.