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

Example Analysis of Redis Protocol

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the example analysis of Redis protocol, which is very detailed and has certain reference value. Friends who are interested must finish it!

The Redis instance runs in a separate process, and the application system and Redis interact with each other through the Redis protocol. Based on the Redis protocol, the client and server can implement a variety of typical interaction modes: serial request / response mode, duplex request / response mode (pipeline), atomized batch request / response mode (transaction), publish / subscribe mode, scripted batch execution (script mode).

Client / server protocol

The interaction protocol of Redis is divided into two parts: network protocol and serialization protocol.

Network interaction

The Redis protocol is on top of the TCP connection, that is, the client and the Redis instance maintain a duplex connection.

The content of the interaction between the client and the server is the serialized corresponding type of protocol data, the server establishes the corresponding connection for each client, maintains a series of states in the application layer and saves them in the connection in the figure above, and the connection is not related to each other. In Redis, connection is implemented through the redisClient structure.

Serialization protocol

In Redis, protocol data is divided into different types, and each type of data ends with CRLF (\ r\ n), which is distinguished by the first character of the data.

Inline command

This type of data represents a Redis command, starting with the character of the Redis command name.

Simple string

The first character is'+', the subsequent character is the content of strig, and the string cannot contain'\ r'or'\ n 'and ends with'\ r\ n'.

Simple string itself does not contain escape, so the deserialization of the client is very efficient, just copy the content between'+ 'and the last two bytes'\ r\ n'.

Bulk string

For cases where string itself contains'\ r'or'\ n', there are two solutions: escape and length self-description. Escape deserialization is inefficient, and Redis uses length self-description, called bulk string.

The first character of the bulk string is'$', followed by the length of the string data, followed by the content of the string itself (which can include special characters including'\ r 'and'\ n'), and ends with'\ r\ n'.

"$12\ r\ nhello\ r\ nworld\ r\ n" the 19 bytes of   describe the two-line string of hello\ r\ nworld.

For an empty string and null, distinguish by the number after'$':

"$0\ r\ n\ r\ n"   these six bytes represent an empty string

"$- 1\ r\ n"   these 5 bytes represent null

Error

The content of data of type error in Redis is a normal string, but the difference from ordinary string is that the first character of error is'-'. The client can judge whether there is an error in this interaction directly by the first character.

Integer

It starts with':', followed by the shaping number itself, and ends with'\ r\ n'. ": 13\ r\ n" represents the integer 13.

Array

It starts with'*', followed by the length of the array, and'\ r\ n' is followed by the serialized data for each element in the array. "* 2\ r\ n+abc\ r\ n 9\ r\ n" means ["abc", 9].

An array length of 0 or-1 indicates an empty array or null, respectively.

Array elements themselves can also be arrays, and multi-level arrays are tree-like structures that are serialized in a way similar to preorder traversal. "* 2\ r\ nroom2\ r\ n 1\ r\ nWan2\ r\ nDev 1\ r\ nhyd1\ r\ n+abc\ r\ n" means [[1,2], ["abc"]].

Protocol data types used by Cpact S

The type that the client sends to the service: inline command, array consisting of bulk string. The type that the server sends to the client is all types except inline command, and it is determined according to the client command or interaction mode.

The above is all the content of this article "sample Analysis of Redis Protocol". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

*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

Internet Technology

Wechat

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

12
Report