Video in progress
What is the difference between Spring's simple broker and an external broker relay (for example the RabbitMQ STOMP plugin), and when do you switch?
The simple broker is an in-memory broker inside Spring: fast and needs no setup, but it works only within a single instance and doesn't store messages. The broker relay forwards messages to an external STOMP broker, so several instances see one shared space of subscriptions.
📖 Answer
The problem with the simple broker appears when you add a second instance: user A is connected to instance-1 and B to instance-2. A message A writes lands in the in-memory broker of instance-1 and never reaches B.
Turning on the relay:
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableStompBrokerRelay("/topic", "/queue")
.setRelayHost("rabbitmq")
.setRelayPort(61613)
.setClientLogin("app").setClientPasscode("secret")
.setSystemLogin("app").setSystemPasscode("secret");
}
Required dependency: spring-boot-starter-reactor-netty (the relay uses Reactor Netty for its TCP connections). On RabbitMQ you need to enable the rabbitmq_stomp plugin.
What to know:
- the relay opens a separate TCP connection to the broker for each client session: 20,000 clients means 20,000 connections on the broker. Size the broker for that;
- destination names follow the broker's rules: RabbitMQ understands the dotted format
/topic/chat.javawell, and behaves differently with/; - with the simple broker
/topicand/queuewere the same; with RabbitMQ they are not.
With a single instance and a small load, the simple broker is enough. The day you need a second instance, you move to the relay (or another shared channel).
Sign up to mark studied questions and save your progress.