Video in progress

STOMP messages are reaching clients late. How do you find the problem in Spring's internal channels and thread pools?

Spring STOMP uses three channels: clientInboundChannel, clientOutboundChannel and brokerChannel, each with its own executor. WebSocketMessageBrokerStats logs their queue sizes and active threads every 30 minutes (more often if configured); that shows which one is jammed.

📖 Answer

The message path: socket → clientInboundChannel (controllers run on these threads) → brokerChannel → broker → clientOutboundChannel → write to the socket.

A stats log line looks roughly like this:

WebSocketSession[1520 current WS(1520)...],
stompSubProtocol[processed CONNECT(1530)-CONNECTED(1530)-DISCONNECT(10)],
inboundChannel[pool size = 16, active threads = 16, queued tasks = 4200, completed tasks = 9813210],
outboundChannel[pool size = 8, active threads = 2, queued tasks = 0, ...]

In this example the diagnosis is clear: all 16 inbound threads are busy and 4200 tasks are queued. The cause is usually slow work inside a @MessageMapping method: a synchronous database query, an external HTTP call. Because the inbound threads are busy, other clients' messages wait in the queue too.

Fixes:

  • take the heavy work out of the controller and hand it to a separate executor or a queue;
  • increase the pool size (configureClientInboundChannel → taskExecutor().corePoolSize(...)), but that only helps if the threads are waiting on I/O;
  • on Java 21, giving it a virtual-thread executor is also an option.

If the queue grows on outbound, the problem is slow clients or too much broadcasting. Then I check sendTimeLimit and sendBufferSizeLimit.

To see the stats, turn on INFO logging for org.springframework.web.socket.config.WebSocketMessageBrokerStats; you can shorten the interval with setLoggingPeriod. Even better is exporting the executor metrics to Grafana through Micrometer.