Thoroughly understand the RSocket protocol

We know that common RESTful Web services all use a request-response interaction method based on the HTTP protocol. This interaction solution is simple, but because it only supports a single interaction method, it cannot cope with all daily development demand scenarios, such as the server actively pushing data to the client.


So, can we have richer interaction methods at the network protocol layer? The answer is yes, and this is the RSocket protocol we are going to discuss today. The RSocket protocol provides a variety of interaction methods between clients and servers. It is not a supplement to the HTTP protocol, but a new, high-performance network communication protocol based on responsive programming technology.
Before introducing the RSocket protocol, you must understand why such a protocol is needed. Let's start with the problems of the traditional request-response model.

What problems does the RSocket protocol solve?

Problems with the request-response model
One problem with the request-response model is that in high-concurrency scenarios, there are bottlenecks in performance and responsiveness. Because the entire processing process is synchronously blocked, if the response time of a request is too long, other requests will not be able to respond in time. I have explained this in detail in the previous lesson on responsive programming.
More importantly, the request-response model provided by the HTTP protocol is not suitable for many application scenarios. A typical example is message push. If the client needs to obtain the latest push message, it must use polling. The client keeps sending requests to the server to check for updates, which undoubtedly causes a lot of resource waste.
You might say that we can use Server-Sent Events (SSE) technology to push messages from the server to the client. However, SSE is also a processing mechanism built on the HTTP protocol, which is generally only used to send text and provides very limited functions.

Fortunately, the industry recognizes the necessity of asynchronous, multi-directional interactive communication. In 2015, the RSocket protocol was born in this context.
RSocket protocol solution
RSocket is a language-independent binary network protocol that solves the single request-response mode and performance problems of existing network transmission protocols. So, how does it solve this problem?

RSocket provides 4 interaction modes in the form of asynchronous messages. In addition to the request/response mode, it also includes three new interaction modes: request/stream, fire-and-forget, and channel.
Let's look at the characteristics of these four interaction modes:

Request-response mode: This is the most typical and common mode. After the sender sends a message to the receiver, it waits for the corresponding response message

Request-response stream mode: Each request message from the sender corresponds to a message stream from the receiver as a response
Fire-and-forget mode: The sender's request message has no corresponding response

Channel mode: A two-way transmission channel is established between the sender and the receiver

We can summarize the above four interaction modes from the corresponding relationship between the number of requests and responses.
As we can see, when we choose a specific interaction mode, the request number of the three interaction modes, request-response, request-response stream, and fire-and-forget, is 1, and different numbers of response results can be obtained. We can reconstruct the existing request processing process based on this feature. The channel mode is more special, and its request number and response number are both N, which determines that we can choose it to deal with bidirectional data flow processing scenarios.
The RSocket protocol is designed to work with reactive programming style applications. When using the RSocket protocol, the data flow mechanism in the reactive programming system is still valid.

To better understand the RSocket protocol, we compare it with the HTTP protocol. In terms of interaction mode, unlike the HTTP request-response one-way interaction mode, RSocket advocates peer-to-peer communication. This peer-to-peer communication is not an improvement on the traditional one-way interaction mode, but the client and the server can freely send and process requests to each other.
On the other hand, in terms of performance, we know that the HTTP protocol is complex and redundant in order to be compatible with various applications, and its performance is average. RSocket uses a custom binary protocol, which is positioned as a high-performance communication protocol, and its performance is an order of magnitude higher than HTTP.

How to use the RSocket protocol correctly?
At this point, we understand what problems the RSocket protocol is trying to solve and how it solves them. Next, let's take a closer look at how to use the RSocket protocol correctly.
RSocket interface
Let's first look at the core interface of the RSocket protocol, namely the definition of the RSocket interface, as shown below.

Obviously, the RSocket interface implements the four interaction modes it provides through four methods. The Payload here represents a message object, which consists of two parts, namely metadata and data, which is similar to the concept of message header and message body in common message communication.
At the same time, did you notice that there are two new data structures Mono and Flux. In reactive programming, Mono represents a data stream containing only 0 or 1 elements, while the corresponding Flux is a data stream containing 0 to n elements.

So the fireAndForget() method returns a Mono stream, which conforms to the semantics of the fire-and-forget pattern. As an implementation of the request-response stream pattern, requestStream() differs from requestResponse() in that its return value is a Flux stream, not a Mono object. Moreover, the request-response pattern provided by RSocket is also more advantageous than HTTP because it is asynchronous and multitasking.
On the other hand, unlike other methods, the requestChannel() method does not return a Payload message object, but a Publisher object representing a responsive stream, which means that the input and output in this mode are both responsive streams, that is, two-way interaction between the client and the server can be achieved, which is consistent with the definition of the channel mode.
RSocket and framework integration
The RSocket interface is too low-level, and developers need to consider the specific construction methods of the server and client and the details of manually implementing remote calls. Therefore, I usually do not recommend using the RSocket interface directly for application development, but prefer to use a specific development framework. If you are using the Spring Boot framework, you can create a simple Controller as shown below:
A new annotation @MessageMapping is introduced here, similar to the @RequestMapping annotation in Spring MVC. @MessageMapping is provided in Spring to specify the destination of message processing in the RSocket protocol. Then, we input a parameter of type String and return a Mono object, which conforms to the definition of the request-response interaction pattern.

In order to access this RSocket endpoint, we need to create an RSocketRequester request object. Based on this object, we can route to the "hello" endpoint constructed earlier through the @MessageMapping annotation through its route() method, as shown below:
Let's look at another example of a request-response flow, as shown below:

Here we return a Flux stream based on the input Request object, sending a new Message object every second.

If you want to use the RSocket protocol in other frameworks, there are many options. Dubbo provides support for responsive programming based on RSocket in version 3.0.0-SNAPSHOT, and developers can use RSocket's API very conveniently. And with the continuous upgrade of the Spring framework, RSocket is also used as the default network communication protocol in version 5.2.
At present, the application of RSocket protocol has become more and more extensive. I believe that as this technology continues to mature, more application scenarios and solutions will emerge in the daily development process.

Summary
Today we systematically discussed RSocket, a new high-performance network communication protocol. Compared with the HTTP protocol, RSocket provides four different interaction modes to achieve diversified network communication. At the same time, RSocket also seamlessly integrates responsive programming technology. We can use the RSocket protocol to achieve asynchronous and non-blocking network communication.