Backend Development 12 min read

Design and Implementation of a WebSocket Long‑Connection Gateway for Real‑Time Push

This article presents the design, implementation, and performance evaluation of a Netty‑based WebSocket long‑connection gateway that centralizes session management, decouples business logic, supports horizontal scaling, and provides monitoring and alerting for high‑throughput real‑time data push in iQIYI’s platform.

High Availability Architecture
High Availability Architecture
High Availability Architecture
Design and Implementation of a WebSocket Long‑Connection Gateway for Real‑Time Push

HTTP is a stateless request/response protocol based on TCP, which cannot proactively push data to clients; scenarios such as message notifications require server‑initiated push. WebSocket, introduced by the HTML5 standard, resolves this limitation and has become the mainstream solution for server push.

iQIYI’s content creation platform uses WebSocket for real‑time comment delivery, identity verification, and live‑body recognition, but faces problems of heterogeneous technology stacks, tight coupling with business services, and lack of session sharing across nodes.

To address these issues, a unified WebSocket long‑connection gateway was built on Netty, offering centralized connection management, business decoupling, a simple HTTP push interface, distributed clustering, multi‑device message synchronization, and multi‑dimensional monitoring and alerting.

Technical selection favored Netty for its high‑performance, event‑driven, non‑blocking architecture. For session sharing, two approaches were considered: a registry‑center mapping and an event‑broadcast model; the lightweight broadcast approach was chosen. Broadcast implementations evaluated included RocketMQ, Redis Pub/Sub, and ZooKeeper, with RocketMQ selected for its high throughput, reliability, and availability.

The gateway’s architecture consists of multiple nodes forming a cluster; each node maintains a memory‑resident connection queue. Clients establish a WebSocket handshake with any node, send periodic heartbeats, and the server cleans up stale sessions. Business systems push data via an HTTP API, which the gateway writes to RocketMQ; all nodes consume the message in broadcast mode and forward it to connected clients if the target session exists.

Session management is handled by a SessionManager component that maps user IDs to UserSession objects, which in turn map channels to ChannelSession objects. When a user exceeds a configurable number of concurrent connections, the oldest ChannelSession is closed to limit resource usage.

Monitoring and alerting are integrated through Micrometer, exposing custom metrics (connection count, user count) to Prometheus and visualizing them in Grafana, with alerts routed to the internal alarm platform.

Performance tests on two 4‑core/16 GB VMs demonstrated the gateway’s ability to maintain up to one million concurrent connections, achieve ~10 s total push latency for a broadcast, and sustain over 1 600 TPS under 600 concurrent requests with ten connections per user.

A concrete business case shows the gateway notifying browsers of image‑filter processing results for iQIYI video covers, reducing integration effort from days to minutes and lowering operational complexity.

In summary, the WebSocket long‑connection gateway provides a reusable, scalable, and observable push solution that decouples communication concerns from business logic, improves development efficiency, and reduces operational costs.

monitoringdistributed architectureNettyWebSocketrocketmqgatewayLong Connection
High Availability Architecture
Written by

High Availability Architecture

Official account for High Availability Architecture.

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.