Reactive Programming with RSocket and Spring Boot

In this article, we’ll explore the RSocket protocol and its use with Spring Boot

Somnath Musib
6 min readAug 5, 2022

--

RSocket is an application protocol for multiplexed, duplex communication over TCP, WebSocket, and other byte stream transports such as Aeron. RSocket allows the following four communication models as shown in following figure 1:

Figure 1 Communication models in RSocket protocol. In the Fire-and-Forget pattern, a client sends one message and expect no response from the server. In the Request-Response pattern, the client sends one message and receives one back from the server. In the Request-Stream pattern, a client sends one message and expects a stream of messages in response from the server. In the Channel pattern, the client and server send streams of messages to each other

In RSocket, once the initial handshake between the client and server is done, the “client” vs “server” distinction is lost as both sides can independently initiate one of the interactions as specified in figure 1.

The RSocket protocol has few key features and offers several benefits:

  • Reactive Streams semantics for streaming requests interactions Request-Stream and Channel. Support for backpressure signal between requester and responder. This allows a requester to slow down a responder at the source. Thus, reduces reliance on network layer congestion control and network-level buffering
  • Support for Request throttling to reduce the number of possible messages. This can be done after sending a LEASE frame to limit the total number of requests allowed by other ends for a given time
  • Fragmentation and re-assembly of large messages
  • Keepalive through heartbeat messages

Next, let us demonstrate how to use the RSocket protocol in a Spring Boot application. We’ll implement all four interaction patterns as shown in figure 1.

TECHNIQUE: Developing applications using RSocket and Spring Boot

Problem

You learned about the RSocket protocol and need to use it in a Spring Boot application

Solution

Spring Framework provides support for the RSocket protocol in the spring-messaging module. Spring Boot provides the spring-boot-starter-rsocket starter dependency that includes the relevant dependencies to use RSocket in a Spring Boot application.

Source Code

If you want to skip to the end and see the final version of the Spring Boot project, it is available at https://github.com/spring-boot-in-practice/repo/tree/main/ch08/spring-boot-rsocket

--

--

Somnath Musib

Software Developer, Cloud Architect | Author "Spring Boot In Practice" . Find more at https://musibs.github.io