Backend Development 13 min read

WebSocket Load Balancing Across Microservice Instances Using a Configurable Annotation

This article explains how to solve the problem of message loss in WebSocket communication within a micro‑service architecture by introducing a library that uses a single configuration annotation to automatically establish inter‑instance connections, manage message routing, and support custom selectors for targeted delivery.

Top Architect
Top Architect
Top Architect
WebSocket Load Balancing Across Microservice Instances Using a Configurable Annotation

WebSocket works well for monolithic applications, but in a micro‑service environment messages may be lost when different instances handle client connections. The author describes a simple solution: forward messages from one instance to another so all clients receive them.

To implement this, a library is created that can be enabled with a single annotation on the Spring Boot entry class:

@EnableWebSocketLoadBalanceConcept
@EnableDiscoveryClient
@SpringBootApplication
public class AServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(AServiceApplication.class, args);
    }
}

In a controller, the WebSocketLoadBalanceConcept bean is injected to send messages across instances:

@RestController
@RequestMapping("/ws")
public class WsController {
    @Autowired
    private WebSocketLoadBalanceConcept concept;

    @RequestMapping("/send")
    public void send(@RequestParam String msg) {
        concept.send(msg);
    }
}

The library abstracts long‑connection clustering by defining a top‑level Connection interface, with implementations such as WebSocketConnection and TCPConnection . A ConnectionSubscriber interface allows services to subscribe to messages from other instances, and can be backed by discovery services, MQ, or HTTP.

Service discovery (e.g., Spring Cloud DiscoveryClient) provides the list of peer instances, and each instance establishes bidirectional connections, sends heartbeat messages, and automatically reconnects when needed. Connections are categorized and managed via a ConnectionFactory and stored in a repository.

Message routing is handled by a ConnectionSelector . The default selector forwards to all connections, but custom selectors (e.g., UserSelector , PathSelector ) enable targeted delivery based on message headers such as user ID or topic path.

Overall, the library offers a plug‑and‑play solution for reliable WebSocket communication in distributed systems, reducing boilerplate code and allowing developers to focus on business logic.

BackendJavaMicroservicesload balancingWebSocketSpring CloudMessage Routing
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.