RSocket explained: request-response, fire-and-forget, request-stream and channel

You've heard of WebSocket. But have you heard of RSocket? It's a protocol that gives a live connection real rules on top. Here are its four ways to talk, the frames behind each one, and how the receiver gets to say "slow down".

Two panels side by side under the heading 'You know WebSocket. Meet RSocket.' Left, WebSocket, a two-way pipe with unlabelled messages going both ways and the note 'what they mean, replies, errors, slow down: you build it all yourself'. Right, RSocket, the same kind of pipe split into four labelled lanes, request-response, fire-and-forget, request-stream and channel, each with its own arrow pattern, and a REQUEST_N valve on top. Note underneath, 4 interaction models, flow control built in, either side can ask, resume after a drop.
Fig. 1 — A WebSocket gives you a pipe. RSocket gives you the pipe plus the rules for talking through it.

You’ve probably heard of WebSocket. It powers chat apps, live scores and stock tickers: one connection that stays open, with messages going both ways.

But have you ever heard of RSocket?

Most developers haven’t. It doesn’t show up in beginner tutorials, and browsers don’t speak it natively. Yet it answers a question every WebSocket user hits sooner or later:

“My connection is open. Now what?”

A WebSocket gives you a pipe. It doesn’t tell you how to match a reply to its question, how to send an error, how to stream a thousand items, or how the receiver can say “slow down, I can’t keep up”. Every team ends up inventing those rules.

RSocket is those rules, written down as a protocol. It defines four ways to talk, called interaction models, and builds flow control into every stream. This article walks through all four, frame by frame, with Spring code for each.

In the last article we placed RSocket among the other interaction protocols and said when it’s worth using. Here we look inside it.

What RSocket is, in one paragraph

RSocket is a binary application protocol for multiplexed, two-way messaging over one connection. It was designed around Reactive Streams, the same idea behind Project Reactor, RxJava and Java’s Flow API. It runs on top of a byte-stream transport such as TCP, WebSocket, Aeron or HTTP/2 streams. Every message is a small frame with a stream ID, so many conversations can share the same connection at once.

Think of it like this:

  • WebSocket is a phone line. You can talk, but there’s no etiquette.
  • RSocket is the phone line plus an etiquette with four kinds of call, a number on every conversation, and a polite way to say “just send me five more for now.”

The four interaction models at a glance

Model You send You get back Everyday picture
Request-Response 1 message 1 message Asking a shopkeeper a question
Fire-and-Forget 1 message nothing Dropping a letter in a postbox
Request-Stream 1 message many messages Subscribing to a newspaper
Request-Channel many messages many messages A live phone call

Here’s what each one looks like on the wire, as the frames that travel between the two sides:

Four sequence diagrams between a requester and a responder. 1, Request-Response: the requester sends REQUEST_RESPONSE, the responder sends one PAYLOAD with the N and C flags; one answer, then done, and the requester may CANCEL. 2, Fire-and-Forget: the requester sends REQUEST_FNF; a dashed line crossed out shows no response frame, ever; best effort, no reply, no CANCEL. 3, Request-Stream: the requester sends REQUEST_STREAM with n equals 2, the responder sends PAYLOAD 1 and PAYLOAD 2, the requester sends REQUEST_N(2) in red, the responder sends PAYLOAD 3 and PAYLOAD 4 with N and C; never more items than requested. 4, Request-Channel: the requester sends REQUEST_CHANNEL with n equals 2, the responder answers with REQUEST_N(1) in red, the requester sends a PAYLOAD, the responder sends two PAYLOADs, then the requester sends COMPLETE and the responder sends COMPLETE; ends when both sides send COMPLETE.
The four interaction models as frames. The red REQUEST_N frames are the receiver saying how many more it can take.

A few words you’ll see on every frame:

  • Requester is the side that starts the conversation. Responder is the side that answers. As you’ll see later, either side of a connection can be the requester.
  • PAYLOAD is a frame carrying a message. Two flags matter: N (next: there is data in here) and C (complete: this is the last one).
  • REQUEST_N is the receiver giving the sender credit: “you may send me N more.”
  • CANCEL says “stop, I don’t need this any more.” ERROR says something went wrong and ends the stream.

Now each model in turn.

1. Request-Response: one question, one answer

This is the model everyone already knows from HTTP. The requester sends one message and waits for one reply.

On the wire:

  1. The requester sends a REQUEST_RESPONSE frame with the question.
  2. The responder sends back one PAYLOAD with both N and C set: here’s the data, and that’s all. Or it sends an ERROR.
  3. The requester can send CANCEL at any time before the answer arrives. The responder then stops, and should not send the response.

Real-world uses: get an order by ID, check a price, validate a coupon. Anything you’d write as a normal REST GET.

How it differs from HTTP: it runs on a connection that’s already open and shared with other streams, so there’s no new connection or request headers for each call. And the cancel is part of the protocol: if the user leaves the page, the work stops on the other side too.

// Java · server side (Spring)
@Controller
class OrderController {
    @MessageMapping("orders.get")
    Mono<Order> get(String orderId) {             // one in, one out = request-response
        return orders.findById(orderId);
    }
}

// Java · client side
Mono<Order> order = requester.route("orders.get")
        .data("A-1042")
        .retrieveMono(Order.class);

In Spring, you don’t name the model anywhere. It’s decided by the shape of the method: one value in, a Mono out, means request-response.

2. Fire-and-Forget: send it and move on

Sometimes you don’t need an answer at all. You just want to tell the other side something.

On the wire:

  1. The requester sends one REQUEST_FNF frame.
  2. That’s it. There is no response frame of any kind: no PAYLOAD, no “OK”, and no ERROR for that message. There’s also nothing to CANCEL, because the stream is finished the moment it’s sent.

The spec calls this best effort. The message goes over a reliable connection, so it normally arrives. But if the responder crashes while handling it, or rejects it, the requester never finds out.

Real-world uses: logging, metrics, analytics events, “user clicked Buy”, “user is typing”. Things where losing an occasional message is fine, and waiting for a reply would just waste time.

// Java · server side
@MessageMapping("events.track")
Mono<Void> track(ClickEvent event) {              // one in, nothing out
    return analytics.record(event);
}

// Java · client side
requester.route("events.track")
        .data(new ClickEvent("buy-button", userId))
        .send();                                  // fire-and-forget: completes once the frame is sent

Why not just use request-response and ignore the reply? Because the responder would still build and send a reply, and the requester would still keep track of a stream waiting for it. Fire-and-forget skips both. When you send millions of tiny events, that adds up.

3. Request-Stream: one request, many answers

Now it gets interesting. The requester asks once, and the responder sends back a stream of messages. It might be ten items, or a million, or an endless feed of live prices.

The danger with streams is obvious: what if the responder is faster than the requester? A server can produce 10,000 price updates a second. A phone can draw maybe 100. Without a rule, the extra items pile up in memory somewhere until something falls over.

RSocket’s rule is simple: the responder may only send as many items as the requester has asked for.

On the wire:

  1. The requester sends REQUEST_STREAM with the question and an initial request N, say 2: “send me up to 2 items.” N must be greater than zero.
  2. The responder sends up to 2 PAYLOAD frames, then must wait.
  3. When the requester is ready, it sends REQUEST_N(2): “2 more, please.” It can send REQUEST_N at any time, and the credits add up.
  4. The stream ends when the responder sends a payload with C set (or an ERROR), or when the requester sends CANCEL.

This is backpressure: the slow side sets the pace, and the fast side waits instead of flooding it. It works like a newspaper subscription where you tell the paper “I’ve read these, send the next batch”, so unread papers never pile up at your door.

Real-world uses: live price feeds, search results that arrive as they’re found, reading a big table in pages, tailing a log, an AI answer streaming in piece by piece.

// Java · server side
@MessageMapping("prices.stream")
Flux<Price> prices(String symbol) {               // one in, many out = request-stream
    return priceFeed.stream(symbol);              // might produce far faster than the client can read
}

// Java · client side
requester.route("prices.stream")
        .data("INFY")
        .retrieveFlux(Price.class)
        .limitRate(50)                            // ask for 50 at a time; sent over the network as REQUEST_N
        .subscribe(this::draw);

That limitRate(50) is the whole trick. In Reactor, a subscriber asking for more is called request(n). RSocket carries that request(n) across the network as a REQUEST_N frame, so the price feed on the server only produces what the phone has asked for.

4. Request-Channel: both sides stream at once

The last model is the most powerful: both sides send streams, at the same time, in one conversation. It’s a live phone call rather than a question and an answer.

On the wire:

  1. The requester opens the channel with REQUEST_CHANNEL. This frame carries the first message and an initial request N: how many messages the requester is ready to receive.
  2. The responder must answer with REQUEST_N, giving the requester credit to send more. The requester may not send further payloads until it gets that credit. Backpressure works in both directions.
  3. Both sides now send PAYLOAD frames, each within the credit the other side has given, and each topping up the other’s credit with REQUEST_N as it goes.
  4. Each side says it’s done by sending a payload with C set. The channel closes when both sides have completed, or sooner if either side sends ERROR (which ends it on both sides) or the requester sends CANCEL.

Real-world uses: chat, multiplayer games, collaborative editing, a phone uploading sensor readings while receiving alerts, a voice assistant streaming audio up while text streams back.

// Java · server side
@MessageMapping("chat.room")
Flux<ChatMessage> room(Flux<ChatMessage> incoming) {   // many in, many out = channel
    return chatRoom.join(incoming);                     // your messages go in, everyone's come back
}

// Java · client side
Flux<ChatMessage> outgoing = typedMessages();          // whatever the user types
requester.route("chat.room")
        .data(outgoing, ChatMessage.class)
        .retrieveFlux(ChatMessage.class)
        .subscribe(this::show);

Channel or two streams? You could open one stream each way. A channel is better when the two directions belong together: they share one ID, one lifetime and one cancel, and backpressure in each direction is part of the same conversation.

How to pick a model

A small decision tree. Top question: what do you need back? Nothing leads to Fire-and-Forget, for logs, metrics and 'user clicked X'. One answer leads to Request-Response, for getting an order or checking a price. Many answers leads to a second question: do you also send many? No leads to Request-Stream, for a price feed or search results. Yes leads to Channel, for chat, games and live sync.
Two questions pick the model: what comes back, and does the requester stream too?

What one connection looks like

All four models share one connection. Here’s what travels through it:

A client and a server joined by one connection, labelled 'one connection (TCP, WebSocket, and so on), many streams inside'. Five rows of frames flow through it: SETUP on stream 0 from client to server; stream 1, a request-response, from client to server; stream 3, a request-stream, from client to server; stream 2, the server asking the client, from server to client; and KEEPALIVE on stream 0 in both directions. Underneath: odd IDs 1, 3, 5 are streams the client starts; even IDs 2, 4, 6 are streams the server starts; stream 0 is the connection itself.
Every frame carries a stream ID, so many conversations can be in flight at once on one connection.
  • SETUP. The client’s first frame. It agrees the protocol version, how often to send keepalives, and the formats for data and metadata (for example JSON, CBOR or Protobuf).
  • Stream IDs. Every stream gets a number. The client uses odd numbers (1, 3, 5…) and the server uses even numbers (2, 4, 6…), so the two sides never pick the same one. Stream 0 is the connection itself.
  • Either side can be the requester. Once connected, there’s no “client asks, server answers” rule. The server can open a stream to the client, for example to ask a mobile app for its current location or to push a config change. That’s what the even stream IDs are for.
  • KEEPALIVE. Small heartbeat frames that prove the other side is still there, even when nothing else is being sent.
  • Data and metadata. Each message has a data part (the actual content) and an optional metadata part for routing, auth tokens or tracing IDs. They can even use different formats. The route names in the Spring examples ("orders.get") travel as metadata.
  • Fragmentation. A big message can be split across several frames, so one large upload doesn’t block every other stream behind it.

Two optional features are worth knowing about:

  • Leasing. The responder can send a LEASE frame saying “you may send me at most N requests in the next T milliseconds.” Requests beyond that are rejected. It’s like rate limiting, but announced in advance, so a busy server can steer callers to its less busy neighbours.
  • Resumption. If a phone goes into a tunnel and the connection drops, the client can reconnect with RESUME instead of SETUP and carry on its streams from where they left off, instead of starting over.

RSocket vs WebSocket vs gRPC

WebSocket RSocket gRPC
What it gives you An open two-way pipe Four interaction models with flow control Typed calls and streams from a .proto contract
Request/reply matching Build it yourself Built in (stream IDs) Built in
Fire-and-forget Just send a message Built in Not a call type; use an empty reply
Backpressure None at the message level REQUEST_N, counted in messages HTTP/2 flow control, counted in bytes
Who can start a call Either side, your own rules Either side, built in The client only
Runs over TCP (starts as HTTP) TCP, WebSocket, Aeron, HTTP/2 HTTP/2
Ecosystem Huge, every browser Small, strongest in Java and Spring Large, many languages

Notice that RSocket can run over WebSocket. That’s how a browser can use it: open a WebSocket, then speak RSocket through it with a JavaScript client library.

When to use RSocket, and when not to

RSocket shines when:

  • The system is streaming-heavy, with long-lived streams rather than one-off calls.
  • The producer can outrun the consumer, and backpressure has to reach all the way back to the source.
  • Your services are reactive end to end, already built on Reactor or similar, so a request(n) in one service can flow through to the next.
  • The server needs to start conversations too, not just answer them.

Stick with something else when:

  • REST is working. For ordinary request-and-reply APIs, HTTP has far better tools, caches, gateways and documentation.
  • You need a typed contract across many languages. gRPC’s .proto files and code generators are much more mature.
  • You only need to push updates to a browser. Server-Sent Events or a plain WebSocket is simpler.
  • The team doesn’t know reactive programming. RSocket’s power comes through Mono and Flux. Without that background, it’s a steep climb.

As we said in the last article: only use RSocket if the backpressure pain is real.

Puzzle: the dashboard that froze phones

Backpressure only works if the client asks for less. If the app subscribes with no limit, Reactor requests the largest possible amount up front, the server is allowed to send everything, and the phone drowns. The fix is on the client: request in small batches with limitRate(...) and only ask for more after drawing what it has.

It’s also worth asking whether a phone needs 5,000 updates a second at all. A human can’t read that fast. Often the better answer is to sample on the server (send the latest price every 200 ms) and let backpressure handle the rest.

Common misconceptions

  • “RSocket is a new kind of WebSocket.” No. WebSocket is a transport RSocket can run over. RSocket is the conversation rules on top.
  • “Fire-and-forget means the message might not be sent.” It’s sent over a reliable connection. What you lose is any confirmation that it was handled.
  • “Backpressure is automatic.” The protocol makes it possible. Your consumer still has to request a sensible amount.
  • “Only the client can make requests.” Once connected, both sides can start any of the four interaction models.
  • “RSocket replaces Kafka.” RSocket moves messages between two live parties. It doesn’t store them. If nobody is connected, nothing is kept.

Key takeaways

  1. RSocket is an application protocol that adds rules to a live, two-way connection: stream IDs, four interaction models and flow control.
  2. Request-Response: one message in, one back, cancellable.
  3. Fire-and-Forget: one message in, nothing back, best effort. Great for events, wrong for money.
  4. Request-Stream: one message in, many back, never more than the requester asked for with REQUEST_N.
  5. Request-Channel: both sides stream at once, with backpressure in both directions, ending when both complete.
  6. Either side can be the requester, and one connection carries many streams, with keepalive, leasing and resumption built in.
  7. Use it when streaming and backpressure are your real problems. Otherwise REST, gRPC or WebSockets are simpler.

Frequently asked questions

What is RSocket in simple words?

RSocket is a protocol for two programs to talk over one open connection using four agreed patterns: ask and get one answer, send without an answer, ask and get a stream back, or stream in both directions. Its key feature is that the receiver can tell the sender how many more messages it can handle.

What are the four RSocket interaction models?

Request-Response (one message in, one out), Fire-and-Forget (one message in, nothing out), Request-Stream (one message in, a stream out) and Request-Channel (streams in both directions at the same time).

What is the difference between RSocket and WebSocket?

WebSocket is an open two-way pipe with no rules about what the messages mean. RSocket is a protocol with rules for replies, streams, errors, cancellation and backpressure, and it can run over a WebSocket, over TCP or over other transports.

How does backpressure work in RSocket?

The receiver of a stream sends REQUEST_N frames saying how many more messages it can accept. The sender must never send more than that total. In Java, this maps directly to Reactive Streams request(n), for example through Reactor’s limitRate.

Is fire-and-forget reliable in RSocket?

The frame travels over a reliable connection, so it normally arrives, but the sender gets no reply and no error. If the receiver fails while handling it, the sender never knows. Use it for logs, metrics and events you can afford to lose now and then.

Can a server send requests to a client in RSocket?

Yes. After the connection is set up, both sides are equal. The client starts streams with odd IDs and the server starts streams with even IDs, and either side can use any of the four interaction models.

Does Spring Boot support RSocket?

Yes. The spring-boot-starter-rsocket starter sets up an RSocket server over TCP or WebSocket, @MessageMapping methods handle requests, and RSocketRequester sends them. Spring picks the interaction model from each method’s input and return types.

Next, see how the four models compare with HTTP, WebSocket and gRPC in communication layer protocols, or how to stop one slow service from taking down the rest in resilience patterns.

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.