Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A STOMP chat works on one instance, but after scaling to three instances users only see messages from people on the same instance. Explain and fix.

Spring's simple broker is an in-memory map of subscriptions inside each instance, so a message sent on instance 1 is delivered only to subscribers connected to instance 1. The built-in fix is enableStompBrokerRelay, which forwards broker destinations to an external STOMP broker such as RabbitMQ or ActiveMQ that every instance shares. Alternatively, we can publish through a shared bus and deliver locally, but the relay is the supported option, and we also check that the load balancer supports WebSocket upgrades and long idle times.

Why this question is asked

This is one of the most common surprises when a Spring WebSocket application is scaled out. Everything looked correct on a laptop, and nothing in the logs shows an error after scaling; messages simply do not reach some users. Interviewers use it to check whether we understand where subscription state lives in Spring's STOMP support. In this answer, we will recap how the simple broker works, reproduce why users only see same-instance messages, switch to the broker relay step by step, look at user destinations across instances and the shared-bus alternative, and finish with load balancer settings.

1A Quick Recap of the Simple Broker

When we call enableSimpleBroker("/topic") in a WebSocketMessageBrokerConfigurer, Spring creates a SimpleBrokerMessageHandler inside our application. It keeps a registry of which WebSocket sessions are subscribed to which destinations, and when a message arrives for /topic/room1, it looks up the subscribers in that registry and sends them the message.

The important word is inside. The registry is an in-memory structure in one JVM. It knows nothing about sessions connected to another instance, and messages sent on one instance never leave it. On a single instance, that is invisible. With three, each instance has its own separate chat room.

2Why Users Only See Messages From Their Own Instance

Suppose Alice is connected to instance 1, Bob to instance 2, and both subscribe to /topic/room1. Alice sends a message. It arrives at instance 1, our @MessageMapping method runs, and the result is sent to /topic/room1 on instance 1's simple broker. That broker has one subscriber, Alice, so she sees her own message and Bob sees nothing. Bob's subscription exists only in instance 2's registry.

Step throughThree instances, three separate brokers
AliceInstance 1 brokerInstance 2 brokerBob
SUBSCRIBE /topic/room1
SUBSCRIBE /topic/room1
SEND /app/chat/room1 hi
MESSAGE hi (only local subscriber)
nothing: instance 2 never saw the message

5 messages in order. Play them, or step through them one at a time.

Each simple broker only knows its own sessions.

3Fix: Relay to an External STOMP Broker

Spring's built-in answer is the STOMP broker relay. Instead of keeping subscriptions in memory, Spring opens TCP connections to an external broker that speaks STOMP, forwards every client SUBSCRIBE and every message for broker destinations to it, and passes the broker's MESSAGE frames back to the right WebSocket sessions. All instances connect to the same broker, so it sees every subscription and every message.

Java
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableStompBrokerRelay("/topic", "/queue")
.setRelayHost("rabbitmq.internal")
.setRelayPort(61613) // RabbitMQ STOMP plugin port
.setClientLogin("chat-client").setClientPasscode(secret)
.setSystemLogin("chat-system").setSystemPasscode(secret)
.setUserDestinationBroadcast("/topic/unresolved-user")
.setUserRegistryBroadcast("/topic/simp-user-registry");
}
// now: Alice on instance 1 sends, Bob on instance 2 receives

A few practical points come with this. The broker must have STOMP enabled; for RabbitMQ that is the rabbitmq_stomp plugin. The relay's TCP client uses Reactor Netty, so reactor-netty must be on the classpath. And destination naming follows the broker's rules: RabbitMQ maps /topic/<name> to its topic exchange with the name as the routing key, so dot-separated names such as /topic/chat.room1 are safer than extra slashes; check your broker's documentation. Spring's side can match dots too by configuring an AntPathMatcher with "." as the separator.

The "system" connection is a single shared connection each instance uses for messages that our code sends with SimpMessagingTemplate, while each client session gets its own relayed connection with the client login.

4User Destinations Across Instances

Private messages need one more piece. convertAndSendToUser("bob", ...) works by resolving Bob's sessions, and the instance sending the message may not have any of them. The two broadcast settings in the code above solve this. setUserDestinationBroadcast publishes messages for users that cannot be resolved locally to a shared topic, so the instance that does hold Bob's session can deliver them. setUserRegistryBroadcast lets instances share which users are connected where, so the user registry, for example used by SimpUserRegistry to list online users, covers the whole cluster.

Without these, group chat works after the relay change but direct messages still fail half of the time, which is confusing to debug.

5The Alternative: A Shared Bus With Local Delivery

We could keep the simple broker and add our own fan-out: every instance publishes each outgoing message to Redis pub/sub or Kafka, every instance subscribes, and each one re-sends the message to its own simple broker with SimpMessagingTemplate. This works and avoids running a STOMP broker, but we now own duplicate prevention (the publishing instance must not deliver twice), ordering, user destinations and failure handling. The relay is the option Spring supports out of the box, so it is usually the right first choice, and the shared bus is worth considering only if the company already runs that bus and does not want a STOMP broker.

Fixing cross-instance delivery
OptionWhat we runWhat we must build
Simple brokernothing extradoes not work across instances
STOMP broker relayRabbitMQ or ActiveMQ with STOMPconfiguration only
Shared bus + local brokerRedis or Kafkafan-out, dedup, user destinations

6Checking the Load Balancer

After the broker fix, the load balancer can still break things. It must support the WebSocket upgrade, which means forwarding the Upgrade and Connection headers; nginx needs these set explicitly for the proxied location. Its idle timeout, often 60 seconds by default, closes quiet connections, so we enable STOMP heartbeats, for example every 10 seconds in both directions, so the connection is never idle for long; the simple broker needs a TaskScheduler for heartbeats, and the relay negotiates them with the external broker. If the application uses the SockJS fallback, which emulates WebSocket over HTTP requests, those requests must reach the same instance, so SockJS needs sticky sessions while native WebSocket does not.

Finally, we test with more than one instance before release: two browsers forced onto different instances and one message. It is a five-minute test that would have caught this bug.

7Follow-up questions

Does the relay make the external broker a single point of failure?

It makes the broker a critical dependency, so we run it as a cluster with the usual high availability setup. Spring's relay reconnects its system connection automatically when the broker returns; client sessions whose relayed connection dropped receive an ERROR frame and must reconnect, so clients need reconnect logic either way.

Can I just use sticky sessions instead?

No. Sticky sessions keep one user on one instance, but two users in the same room are still on different instances, and their messages must cross between them. Stickiness does not share subscriptions; only a shared broker or bus does.

Why does group chat work after the relay change but direct messages do not?

Because user destinations are resolved by the sending instance's user registry. If Bob is connected elsewhere, the message cannot be resolved locally. Enabling setUserDestinationBroadcast and setUserRegistryBroadcast lets unresolved messages and registry information travel between instances through the broker.

8Conclusion

In this answer, we saw that Spring's simple broker keeps subscriptions in each instance's memory, so after scaling to three instances messages only reach subscribers on the same instance. The built-in fix is enableStompBrokerRelay to an external STOMP broker such as RabbitMQ or ActiveMQ, plus the user destination and registry broadcasts for private messages. A shared bus with local delivery is an alternative with more code to own, and the load balancer must support WebSocket upgrades, with STOMP heartbeats to survive idle timeouts.

What to say in the interviewAbout 90 seconds aloud

Open with

The simple broker from enableSimpleBroker is an in-memory subscription registry inside each JVM. On one instance that is fine, but with three, each instance has its own registry, so when Alice on instance 1 sends to /topic/room1, only subscribers connected to instance 1 get it; Bob's subscription lives on instance 2 and never sees the message.

Then, if they want more

Nothing fails, so there are no errors in the logs. The built-in fix is to replace the simple broker with enableStompBrokerRelay, pointing at an external STOMP broker like RabbitMQ with the STOMP plugin, or ActiveMQ. Spring then forwards subscriptions and broker messages over TCP to that broker, which all instances share, so every subscriber gets every message.

I would need reactor-netty on the classpath, use dot-separated destinations that fit RabbitMQ's routing, and for convertAndSendToUser across instances I would set the user destination broadcast and user registry broadcast, otherwise direct messages still fail when the user is on another instance.

The alternative is to publish every message to a shared bus like Redis and redeliver locally on each instance, but then I own deduplication and user destinations, so the relay is the better first choice.

Finally I would check the load balancer: it must pass the WebSocket upgrade, and its idle timeout needs STOMP heartbeats; SockJS would also need sticky sessions.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A STOMP chat works on one instance, but after scaling to three instances users only see...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/367b18bdbb/