RSocket is an efficient binary network communication protocol that can be used in many scenarios. RSocket is fully reactive; even its APIs are integrated seamlessly with reactive programming.
This mode supports sending a request without sending a response message. It can be applied to scenarios, such as event tracking and log reporting. A response in these scenarios is not needed, and it does not matter if several requests are lost.
The requestor sends a request, and the responder receives the request and returns a response. HTTP typically uses the RequestResponse
mode.
The requester sends a request, and the responder returns N responses. Conventional MQ typically uses the RequestStream
mode.
Create a channel context so both parties can send messages. IM is a typical RequstChannel
scenario.
The process of the Client in RSocket connecting to the Server is called Setup. After the connection is established, the direction logic of message sending and receiving is specified:
Unlike traditional HTTP requests, RSocket can implement bi-directional communication based on the feature that says parity determines the direction.
In summary, RSocket is more efficient and supports more scenarios than HTTP. It eliminates head-of-line blocking. Compared with event-based frameworks, such as SocketIO, RSocket requests have a clear context, are refined, and APIs are easy to use.
Frame is the minimum unit of RSocket message.
Developers generally deal with Payload, which is similar to an HTTP message. Payload can be a request or response. It consists of two binary parts:
The following figure shows the architecture based on my experience in implementing the Golang SDK. The architecture in Java is similar:
RSocket can be used in many scenarios, such as in traditional RPC. It also can be used in IM. Some of its features can be used in proxy or network penetration.
It can also be used in IoT scenarios. For example, Xiao Ming has a smart air conditioner at home. Xiao Ming wants to control the air conditioner when he is outside through a mobile phone app. How can he solve this control problem gracefully? The most refined solution is "Xiao Ming calls the API of the switch of the air conditioner."
Moreover, the most typical application of RSocket is Broker, which is similar to a soft route solution that simplifies service release and access. It can connect to the Broker to release services. The caller can make the Broker forward data transparently using reverse requests. Traditional service governance methods, such as registry and port management, are abandoned in this process.
Broker has many advantages. For example, port listening is not required for releasing services, and Sidecar is not required. Service registration becomes easier. Load balancing becomes easier and safer as zk and etcd are not required. It is less likely to be attacked without a listening port, but there are also a lot of disadvantages. It has some performance losses. Broker is a centralized design similar to global NGINX, but the elegant start-stop of Broker is more complex and limited by the bottlenecks of the entire Broker cluster. In a word, nothing is given without a disadvantage in it.
Many FaaS services implemented by AMap use Broker based on the RSocket architecture. This enabled AMap to handle the large network traffic during the 2021 May Day holiday. The peak QPS exceeded 200,000, while its system remained stable with zero faults.
Here, I prepared a Mini Broker to show its mechanism, which demonstrates the scenario where two browsers call each other's services in context. If you are interested, you can refer to the following figure:
Reactive programming is very common and has been everywhere for a long time. Even the SUM function in Excel is essentially reactive. Reactive programming is to respond to changing data streams. RSocket is based on reactive programming but has extended it to the network layer.
In our daily work, various operations and transformations are bound to be introduced:
JDK has launched reactive standard APIs. Apart from the Processor, its core interfaces only contain Publisher, Subscriber, and Subscription.
Spring Reactor is a standard implementation, and its complete execution process is listed below:
onSubscribe
.onNext
for each successfully produced data entryonError
and end the current subscription.onComplete
and end the current subscription.request(n)
. This process is called backpressure.Since there are inherent advantages, Java is suitable for frameworks, such as RxJava or Reactor, and the code logic is clearly readable. When implementing Go Reactor, I realized that the expressiveness of APIs without generic support is poor. I also hope that the generics of Go2 can be improved.
RSocket is a very interesting network protocol. It may not be popular, but its problem-solving ideas and design are very innovative. If you are interested, you can visit its official website to learn more.
This article summarizes some of my thoughts when implementing RSocket SDK for Go and Rust. If you are interested, click the relevant links below.
hujt - April 1, 2021
Alibaba Cloud Community - March 9, 2022
Aliware - January 4, 2021
Alibaba Cloud Community - March 9, 2022
Alibaba Developer - January 10, 2020
Alibaba Cloud Community - March 9, 2022
Explore Web Hosting solutions that can power your personal website or empower your online business.
Learn MoreA low-code development platform to make work easier
Learn MoreOpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.
Learn MoreExplore how our Web Hosting solutions help small and medium sized companies power their websites and online businesses.
Learn More