Backend Development 12 min read

WebSocket Load Balancing Concept for Microservice Architecture

This article explains the challenges of using WebSocket in a micro‑service environment, introduces a load‑balancing library with annotation‑based configuration, and details its abstract design, connection management, message routing, and selector mechanisms for targeted and broadcast communication.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
WebSocket Load Balancing Concept for Microservice Architecture

When a WebSocket client connects through a gateway in a micro‑service setup, load balancing may route the client to one service instance (e.g., A1), causing messages sent from another instance (A2) to miss the client. A simple fix is to forward messages between instances so the client receives them.

The library provides an annotation @EnableWebSocketLoadBalanceConcept to enable this functionality. After adding the annotation to the Spring Boot application, the library automatically creates connections between service instances and allows cross‑instance message sending.

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

Inject WebSocketLoadBalanceConcept where messages need to be sent:

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

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

The design abstracts a top‑level Connection interface, with implementations such as WebSocketConnection and TCPConnection . Connections are classified as Client (normal), Subscriber (receives forwarded messages), and Observable (sends forwarded messages).

A ConnectionFactory adapts any connection type, while MessageEncoder and MessageDecoder handle encoding/decoding. Different connection categories use distinct codecs, coordinated by MessageCodecAdapter .

Service instances discover each other via Spring Cloud discovery (Eureka, Nacos, etc.) using DiscoveryClient#getInstances . Upon connection, instances exchange heartbeat messages and automatically reconnect if a link is lost, providing resilience.

Message routing is controlled by a ConnectionSelector . Custom selectors can filter connections by metadata, headers, user ID, or path. The library includes ready‑made selectors such as UserSelector and PathSelector , which can be enabled via configuration.

For targeted delivery, a client sends its userId on connection; the server stores this in the connection’s metadata. When a message contains a matching userId header, the selector routes the message only to the appropriate connection, avoiding unnecessary broadcasts.

The article concludes that the core concept— ConnectionLoadBalanceConcept —acts as the “Dao” of the system, with connections, messages, repositories, and selectors forming the supporting structure, enabling scalable and flexible WebSocket communication across micro‑services.

Javamicroservicesload balancingWebSocketSpring CloudMessage Routing
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.