redis client protocol 解析,redisprotocol

來源:互聯網
上載者:User

redis client protocol 解析,redisprotocol

在官網http://redis.io/topics/protocol有對redis通訊協定有做說明。
基於下面的一些原因,我想解析redis client protocol:

1、足夠瞭解通訊協定,有助於做出更好的系統設計。

2、學習RESP的設計思想,不僅能擴充我的思維,也許將來能應用於My Code中。

3、因為有些人想將redis client直接併入自己已有的系統中;包括我在內。這個將在我下一篇文章再做說明。

下面我翻譯一下http://redis.io/topics/protocol一些我認為重要的內容:

Redis clients communicate with the Redis server using a protocol called RESP (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.

譯:

redis用戶端和redis服務端用一種名叫RESP(REdis Serialization Protocol)的協議通訊。雖然這種協議專門為redis設計,但是它能被用於其它基於C/S模型的軟體項目中。

RESP是基於下面一些事實的一種折衷方案:

  • 易於實現
  • 快速解釋
  • 人類可讀

RESP能序列化不同的資料類型,例如整型,字串,數組,還有專門的錯誤類型。用戶端發送字串數組請求到服務端,而字串數組表示命令參數去執行。Redis會用專門的命令類型回複。

RESP是二進位安全的,同時過程轉換中不需要大量的資料處理,因為它使用了前置長度去轉換批量資料。

注意:在這裡概述的協議只用於用戶端-服務端通訊。而Redis叢集為了不同節點交換訊息使用了一種不同的二進位協議。

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:

  • For Simple Strings the first byte of the reply is "+"
  • For Errors the first byte of the reply is "-"
  • For Integers the first byte of the reply is ":"
  • For Bulk Strings the first byte of the reply is "$"
  • For Arrays the 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).

譯:

RESP實際上是一種支援下面資料類型的序列化協議:短字串,錯誤,整數,長字串和數組。

RESP作為一種請求-回應協議,在Redis中的使用方法如下:

  • 用戶端發送一種猶如RESP中長字串數組的命令到Redis服務端。
  • Redis服務端根據命令實現回複其中一種RESP類型。

在RESP中,一種資料類型基於第一個位元組:

  • 對於短字串,回複的第一個位元組是"+"
  • 對於錯誤,回複的第一個位元組是"-"
  • 對於整數,回複的第一個位元組是":"
  • 對於長字串,回複的第一個位元組是"$"
  • 對於數組,回複的第一個位元組是"*"
另外RESP能用指定的長字串或數組的特殊變數來表示空值。
在RESP中,協議的不同部分總是以"\r\n"(CRLF)作為結束。

前面說到的協議,我有強調了是client,就是說server回複client請求時用到的協議;client請求server時,只需要在命令後面加上"\r\n"。

下面是5種類型的返回執行個體:

假設在redis server中存在以下索引值對:name1catage110短字串"set name2 fish\r\n""+OK\r\n"錯誤"seet name3 dog""-ERR unknown command 'seet'\r\n"整數"incr age1"":11"長字串①"get name1\r\n""$3\r\ncat\r\n"②"get name3\r\n""$-1\r\n"數組①"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"③其它情況會返回"*-1\r\n"和"*0\r\n",具體參考redis官方文檔;

相關文章

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.