Let’s imagine that we have a simple chat application in which the frontend communicates with the backend via rest and WebSockets used for chatting. We realize that one instance of the application starts not to cope with the load.

It’s not a trivial task to scale microservices that uses WebSockets. With a simple launch of another instance under a default round-robin load balancer, we may get a situation when one user connects to instance-A and the second user connects to instance-B. And now, our backends must somehow understand where to send incoming messages.

Custom Load Balancer

The first option that may come to mind is to write an intelligent load balancer that will redirect users from the same chat to the same instance.

Several problems can arise here:

  1. If users communicate with many people simultaneously, then for each chat, you need to open a new WebSocket connection.
  2. If there are too many people in the chat, then one backend instance may not cope. For a chat specifically, this is unlikely, but the chat example is an oversimplification. In real life, this problem is not uncommon.

#spring-boot #java #websocket #microservices

Scaling WebSockets in Spring services
1.65 GEEK