Implementing a Netty Cluster Long‑Connection Service with Redis Pub/Sub for Message Forwarding
This article explains how to build a high‑concurrency Netty long‑connection server cluster using Redis publish/subscribe to forward messages across nodes, detailing the background, technology choices, architecture, and implementation steps for a microservice‑based backend system.
Background – The company needed to develop its own long‑connection server for a social project’s audio/video features, choosing SpringBoot + Netty within a microservice architecture to ensure high concurrency and reliable connections.
Netty Long‑Connection – Netty, an asynchronous NIO framework, can serve as a long‑connection server using the WebSocket protocol, supporting roughly ten thousand connections per instance. In a cluster, client channels may reside on different nodes, requiring node‑aware message routing.
Technology Options – For inter‑node message forwarding, the team considered Zookeeper, MQ, and Redis. Since MQ and Redis were already in use, they evaluated both: MQ offers high reliability but is heavyweight and asynchronous; Redis provides lightweight, low‑latency, high‑concurrency pub/sub with lower reliability. Given the real‑time nature of live‑stream messages, Redis pub/sub was selected.
Architecture – When the service starts, each node registers itself in Redis and subscribes to a specific channel. After a client connects, the node binds the user’s session to its identifier in Redis. When a message arrives, the node checks if the target user is on the same node; if not, it retrieves the target node’s info from Redis and publishes the message to the appropriate channel, where the target node delivers it to the client.
Implementation Overview – The article includes diagrams (omitted here) and describes the key steps: node registration and listener setup, subscription handling, channel listener processing, and a message‑forwarding sender component.
Conclusion – Using Redis pub/sub with a Netty cluster enables automatic node registration and seamless message forwarding across newly added nodes, offering strong scalability. The author also notes ongoing learning of advanced Redis 5 features for further business scenarios.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.