Backend Development 14 min read

WebSocket Load Balancing Library for Microservice Architecture

This article introduces a Spring Boot library that uses a configuration annotation to enable WebSocket load balancing across microservice instances, explains its abstract design, connection management, message routing, and provides usage examples with code snippets while also mentioning related promotional content.

Top Architect
Top Architect
Top Architect
WebSocket Load Balancing Library for Microservice Architecture

WebSocket is familiar to most developers, but when used in a microservice architecture, message delivery can fail if a client is connected to one instance while another instance needs to broadcast a message.

The simplest solution is to forward messages from the sending instance to the instance that holds the client connection, and then deliver the message to the client.

Based on this idea, the author created a library that configures everything with a single annotation.

Usage

First, add the annotation to the Spring Boot application class:

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

Then 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 library abstracts long‑connection clustering by defining a top‑level Connection interface, with implementations such as WebSocketConnection and TCPConnection . It also supports other protocols like HTTP via custom ConnectionSubscriber implementations.

Service discovery is handled through Spring Cloud; the library obtains all service instances via DiscoveryClient#getInstances , excludes the current instance, and establishes mutual connections between instances.

Connections are categorized and managed through a ConnectionFactory , with each connection having a MessageEncoder and MessageDecoder . A MessageCodecAdapter adapts different codecs for forwarding and client messages.

Message sending can be targeted using selectors. The library provides a ConnectionSelector to choose connections based on criteria such as user ID or path. Inline code examples show how to send messages to specific users by storing userId in connection metadata and matching it in message headers, as well as how to use a PathSelector for topic‑based routing.

The article also includes promotional sections advertising a knowledge community, AI‑related courses, and interview material, encouraging readers to join for additional resources.

JavaMicroservicesload-balancingSpring BootWebSocketConnection Management
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.