RSocket is an efficient binary network communication protocol that can be used in many scenarios. In addition, RSocket is a radical defender of responsiveness, so radical that even the API integrates seamlessly with responsiveness. In this article we will share RSocket and reactive programming.

The author crossing the source | | element ali technology to the public

A main feature of RSocket

First of all, RSocket is an efficient binary network communication protocol, which can be used in many scenarios. Second, RSocket is a radical defender of responsiveness, so radical that even the API integrates seamlessly with responsiveness.

Four modes of communication

Fireand forget

Send a request immediately without sending a response message for the request. It is suitable for monitoring buried sites, log reporting, etc. No return receipt is needed in this scenario, and it is harmless to lose a few requests.

Request response RequestResponse

The requester sends a request message, and the responder receives the request and returns a response message. Traditional HTTP is typically RequestResponse.

Flow RequestStream

The requester sends one request message, and the responder sends N response messages back. Traditional MQ is a typical RequestStream.

Channel RequestChannel

Create a channel context where both parties can send messages to each other. IM is a typical RequestChannel communication scenario.

Bi-directional communication

The RSocket Client connects to the Server in a process called Setup. Upon successful connection, the logic for sending and receiving messages is specified:

  • When a Client requests a Server, the request ID sent is always an odd number
  • When the Server requests a Client, the request ID sent is always an even number



Because of this directional feature, RSocket can make two-way requests, unlike traditional requests such as HTTP.

Three other

  • Binary protocol, compact and efficient
  • multiplexing
  • Frame – based back pressure, and ReactiveStreams semantics
  • Flexible transport layer switch: TCP/UDP/WebSocket, etc
  • Support for advanced features such as Cancel, Breakpoint Continues, and Leases

In summary, compared to HTTP, RSocket is more efficient, supports a wider range of communication scenarios, and does not have the problem of queue header blocking. In contrast to a pure event-based framework such as Socketio, RSocket requests have a clear context and an API that is refined and easy to use.

Internal implementation of RSocket

1 frame design

A Frame is the smallest unit of RSocket packet.

  • A Frame consists of 6 bytes of headers and the rest of the Body, in which 4 bytes of headers represent Streamid, 6 bits represent Frame Type and 10 bits represent Flags. The structure of the Body varies according to different frame types. A commonly used frame with Payload usually contains two parts: Metadata and Data.
  • If the transport layer itself does not support framing (such as TCP), then RSocket will use 3 bytes of UINT24 for frame length, so the maximum frame size is 16MB.
  • If a frame is larger than 16MB, RSocket supports frame splitting and regrouping, which means that smaller frames are split and reassembled automatically by the receiver.

2. Data carrier — Payload

Based on the frame, the Payload is a Payload, which is similar to an HTTP message and can be either a Request or a Response. Consisting of two binary parts:

  • Metadata — Metadata, similar to HTTP headers
  • Data — Data, similar to the body of HTTP

3 architecture

This is based on the architecture diagram I put together based on implementing the Golang SDK, and the Java version is similar.

  • The Transport layer encodes and decodes the network binary stream into Frames.
  • RSocket supports a custom maximum Frame Size, default is 16MB. When a Frame is exceeded, it is fragmented into N small frames and reconfigured when received. This feature is called Fragmentation as mentioned in the introduction to frames.
  • DuplexConnection transforms Frames into payloads, abstracts them into individual Request/Response contexts, and is responsible for reading and writing.
  • RSocket assembly connections can be used as RSocket interfaces, where a Resumable can be used to close and recuperate connections. RSocket assembly connections can be used as RSocket interfaces, where a Resumable can be used to recuperate and recuperate connections. So it consumes a lot of system resources.
  • RSocket uses the Reactor core library to expose four communication modes, abstracted as high-level APIs.

4 play

RSocket has a number of ways to play it. Traditional RPC is fine, IM is fine, and some features can also be used for proxies or network penetration.

In the IoT scene, for example, Xiaoming has an intelligent air conditioner in his home, and he wants to control the switch of the air conditioner through the mobile phone APP outside. How to describe this control problem gracefully? The most refined solution is “Xiao Ming calls the API of the switch on the air conditioner”.

Another classic technique is the Broker, which acts as a “soft routing” solution that makes it easy to publish and access services. Publish the service only to connect to the Broker, and the caller makes the transparent forwarding of the Broker through the reverse request way, eliminating the traditional registry, port management and other common service governance means.

About RSocket Broker

Broker has many advantages, such as no port listening for publishing services, no Sidecar, easy registration of services, no ZK, ETCD, etc. LoadBalance is simpler and more secure, and harder to attack if no port listening is required. There are a number of disadvantages, such as a jump on the network and performance loss. Broker is centrally designed, similar to the global Nginx, but the elegant start and stop of the Broker is much more complex, limited by the bottleneck of the entire cluster of brokers. When God closes a door for you, he will open a window for you.

At present, AutoAutoland FAAS used a large number of group brokers based on RSocket architecture, supporting this year’s May Day holiday, peak QPS over 200,000, stable zero failure.

Here I have prepared a Mini Broker for teaching. It demonstrates the scenario of two browsers calling each other’s services in context. Those who are interested can check it out.

Three response programming

Responsive programming is an old topic, it’s been around for a long time, and even when you SUM things up in Excel, it’s essentially reactive thinking. A responsive expression is essentially a flow of data that responds to change. The RSocket protocol itself extends to the network level in the name of responsiveness.

1 Reactive programming looks something like this

In our daily work, we will inevitably introduce a variety of operations and transformations:

2 Reactive Streams

JDK launched response type standard API, regardless of Processor, its core interface is Publisher/Subscriber/Subscription, very refined.

  • Publisher: The person who is responsible for the production data. The only way to subscribe is to receive a Subscriber to start a new subscription.
  • Subscriber: A Subscriber who is responsible for subscribing consumption data.
  • Contextual control of a Subscription, such as cancellation, notification to retrieve the next N pieces of data.

Spring Reactor is a standard implementation. The complete execution process is shown in the following figure:

  • Create a Subscriber and start subscribing to Publisher.
  • Generate context subscription.
  • Publisher ready, call onSubscribe.
  • Publisher starts producing data.
  • OnNext is called back for each successfully produced piece of data.
  • When production fails, you call back to OnError and end the current subscription.
  • When all data is produced, call back onComplete and end the current subscription.
  • The subscription can be cancelled at any time during the process, or the next n elements can be produced via request(n) notification.

Due to Java’s natural language advantages, frameworks such as RxJava or Reactor are ideal for code that is logical and readable. When implementing the GO version of Reactor, the author deeply realized how poor the API performance was without generic support, and expected that the GO2 generic would be improved.

Four summarizes

RSocket is a very interesting network protocol, it may not be popular, but its problem solving and design are very refreshing. If you are interested, you can go to its official website.

The original link

This article is the original content of Aliyun, shall not be reproduced without permission.