Chat Backend (Realtime Service)

  • Backend
  • Realtime

Production · Node.js, Realtime, Docker, Redis (Pub/Sub) …

Executive Overview

A horizontally scalable realtime messaging service engineered with Node.js, WebSockets, and a Redis Pub/Sub distributed backplane, designed for low-latency multi-room chat, presence detection, and message broadcast.
The Challenge & Bottleneck

Core Problem

Realtime socket architectures face scalability bottlenecks when scaling beyond a single server instance: clients connected to Server A cannot communicate with clients on Server B without a shared pub/sub layer. Additionally, abrupt network dropouts and reconnection storms can lead to memory leaks, connection exhaustion, and out-of-order message delivery.

Engineering Approach

Architectural Solution

Architected a completely stateless WebSocket server cluster utilizing a Redis Pub/Sub adapter to synchronize channels across all container nodes. Implemented heartbeat ping/pong keep-alives, client-side message sequence numbering to detect dropped packets, and aggressive connection draining during graceful server shutdowns.

Quantified Outcomes

Measurable Impact

Successfully scaled concurrent socket connections across multiple container replicas with sub-20ms message delivery latency, eliminating split-brain chat rooms and preventing socket leakages during high-churn reconnect events.

System Architecture

Component topology, protocol boundaries, and data flow.

Realtime Cluster & Pub/Sub Architecture
Horizontally Scalable
CLIENT APPSWeb / MobileWebSocket ConnectionsBidirectional FramesINGRESS / NGINXTLS TerminationSocket UpgradeSticky SessionsNODE.JS SOCKET CLUSTER• Stateless Socket Pods• Frame Rate Limiting• Heartbeat Keepalive• Clean Disconnect DrainREDIS & DATABASERedis Pub/SubCross-Node SyncPersistent DatastoreChat Message History

Reliability & Production Security

Enforced strict per-socket frame rate limiting to mitigate DDoS flooding attacks, payload schema validation with maximum byte size caps, and secure JWT-based authentication during initial WebSocket handshake. Redis pub/sub backplane configured with automatic failover and reconnect backoff.

Deployment & Infrastructure

Dockerized Node.js service running behind an AWS Application Load Balancer configured for sticky sessions and HTTP/1.1 WebSocket upgrade tunneling. Deployed as scalable container tasks with health checks validating both HTTP readiness and Redis adapter connectivity.
Engineering Post-Mortem & Insights

What I Learned

Technical trade-offs, battle-tested discoveries, and operational takeaways from this project.

1

Stateless Socket Nodes Are Essential for Horizontal Scaling

Realtime nodes must never store local room state in memory. Delegating room distribution to a centralized Redis pub/sub backplane allows instances to scale up or down without disrupting user conversations.

2

Guard Against Reconnection Storms with Exponential Backoff

When a server restarts, thousands of clients attempting simultaneous reconnection can overwhelm the authentication layer. Enforcing client-side jittered backoff prevents thundering herd crashes.

3

Graceful Connection Draining Prevents Memory Leaks

Abruptly terminating socket processes leaves dangling client buffers. Intercepting SIGTERM signals to send a reconnect notice to clients and allowing 5 seconds for sockets to close cleanly guarantees zero lost messages during deployments.

4

Strict Rate Limiting on WebSocket Frames

Unlike HTTP APIs protected by reverse proxies, raw WebSocket connections can easily be abused with rapid payload floods. Frame-level token bucket rate limiters per connection are mandatory.

Future Roadmap & Architectural Evolution

  • →Adopt Redis Streams or Apache Pulsar to provide persistent at-least-once message history and offline catch-up.
  • →Implement WebRTC signaling endpoints for direct peer-to-peer audio and video streaming.
Chat Backend (Realtime Service) | Siddhant Ghosh