Introduction: 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 apis are seamlessly integrated with responsiveness. In this article we will share with you RSocket and reactive programming.

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

Key features of RSocket

First, RSocket is an efficient binary network communication protocol that can be used in many scenarios. Second, RSocket is a radical defender of responsiveness, so radical that apis integrate seamlessly with responsiveness.

Four modes of communication

FireAndForget

Send a request immediately without sending a response packet for the request. It is suitable for monitoring buried points and reporting logs. In this scenario, no receipt is required and several requests are lost.

RequestResponse RequestResponse

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

Flow RequestStream

The requester sends one request packet, and the responder sends back N response packets. 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 Client of the RSocket connects to the Server. This process is called Setup. After the connection is successful, the direction logic for sending and receiving messages is agreed:

  • When a Client requests a Server, it always sends an odd number of request ids
  • When a Server requests a Client, the request ID is always an even number

Because of this parity orientation, unlike traditional HTTP requests, RSocket can make two-way requests.

Three other

  • Binary protocol, compact and efficient
  • multiplexing
  • Frame based back pressure, and ReactiveStreams semantics fit
  • Flexible transport layer switching: TCP, UDP, WebSocket, etc
  • Supports advanced features such as Cancel, resumable breakpoint, and lease

In summary, compared with HTTP, RSocket is more efficient, supports more diverse communication scenarios, and does not have the problem of queue head blocking. Compared to SocketIO, a pure event-based framework, RSocket requests have a very clear context, and the API is refined and easy to use.

Two RSocket internal implementation

1 frame design

Frame is the smallest unit of RSocket packets.

  • A Frame consists of a 6-byte Header and the rest of the Body, where 4 bytes of the Header denote StreamID, 6 bits denote Frame Type, and 10 bits denote Flags. The Body structure varies according to frame types. Commonly used frames with Payload include Metadata and Data.
  • If the transport layer itself does not support frame splitting (such as TCP), then the RSocket uses a 3 bytes uint24 to represent the frame length, so the maximum frame size is 16MB.
  • If the frame exceeds 16MB, RSocket supports frame splitting and regrouping, which is to break the frame into smaller frames and regroup them automatically at the receiver.

2 Data carrier Payload

Based on frames, developers are exposed to Payload, which is similar to an HTTP packet. It can be a Request or a Response. Consists of two binary parts:

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

3 architecture

This is based on the architecture diagram compiled by the author based on the implementation of Golang SDK, and the Java version is similar.

  • The Transport layer encodes and decodes the network binary stream to Frames.
  • RSocket supports a customized maximum Frame Size, which is 16MB by default. If a Frame exceeds its Size, it is divided into N smaller frames and assembled when received. This feature is called Fragmentation.
  • DuplexConnection converts Frames to Payload, abstracts each Request/Response context, and is responsible for reading and writing.
  • RSocket assembly Connection to RSocket Interface, including Resumable support breakpoint continuingly, disconnects reconnection also can self-healing, personal feel this feature a little chicken ribs, in weak network environment has some advantages, but because caches live not processed during the period of the frame, So it consumes a lot of system resources.
  • RSocket uses the Reactor core library to expose four communication modes, abstracted into a high-level API.

4 play

There are many ways to play RSocket, including traditional RPC, IM, and some features that can also be used for proxy or network penetration.

IoT scenario, for example, xiaoming has a smart air conditioner in his home, and xiaoming wants to control the air conditioner switch by using a mobile phone APP outside. How to describe this control problem elegantly? The most refined solution is “Xiao Ming calls the API on the switch on the air conditioner”.

Another classic approach is the Broker, which is a kind of “soft routing” scheme that makes it easy to publish access to services. To publish a service, you only need to connect to the Broker, and the caller can make the Broker forward transparently through a reverse request, abandoning traditional registry, port management and other common means of service governance.

5 About RSocket Broker

The Broker has many advantages. There is no need to listen on ports to publish services, no need for Sidecar, simple service registration, no need for ZK, ETCD, etc. LoadBalance is simple and more secure. It is difficult to attack without listening on ports. The Broker is a centralized design, similar to our usual global Nginx, but the elegant start and stop of the Broker is obviously more complex, limited by the bottleneck of the whole cluster of brokers, etc. When God closes a door, he will open a window for you.

At present, a large number of Group brokers based on RSocket architecture are used in the FaaS launched by Autonavi, which supported this year’s May Day holiday with peak QPS of more than 200,000 and stable zero failures.

Here, the author also prepared a Mini Broker for teaching, demonstrating the scenario of two browsers calling each other’s services in the context of each other. Interested students can check it out.

Three responsive programming

Reactive programming is an old topic, it’s so ubiquitous that even when you SUM up in Excel, it’s essentially reactive thinking. Reactive is essentially a flow of data in response to change. RSocket itself is a protocol that extends it to the network level in the name of responsiveness.

Reactive programming looks something like this

In our daily work, various operations and transformations will inevitably be introduced:

2 Reactive Streams

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

  • Publisher: The Publisher responsible for producing data. The only way to subscribe is to receive a Subscriber to start a new subscription.
  • Subscriber: the Subscriber who is responsible for subscription and consumption data.
  • Subscription: Context control of a Subscription, such as cancellation, notification of the next N entries

Spring Reactor is a standard implementation, and its complete implementation process is shown below:

  • Create subscriber and subscribe to Publisher.
  • Generate context subscription.
  • Publisher is ready. Call onSubscribe.
  • Publisher starts producing data.
  • Call onNext for each successfully produced piece of data.
  • When production fails, onError is called back and the current subscription ends.
  • When all data is produced, call onComplete and end the current subscription.
  • Subscription can be called to cancel at any time midway, or to notify production of the next n elements via Request (n), a process known as back pressure.

Due to Java’s natural language strengths, using a framework like RxJava or Reactor makes the code logical and readable. When I implemented Reactor for Go, I was impressed by the lack of expressiveness in apis without generics support, and I look forward to improving generics in Go2.

Four summarizes

RSocket is an interesting network protocol that may not catch on, but it’s refreshing in its problem-solving and design. If you are interested, you can go to its official website.

The original link

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