Chat Service System Design: 500k WebSockets, Epoll Limits & Pub/Sub Routing
A complete architectural breakdown of a Discord / Slack / WhatsApp scale real-time messaging cluster: stateful TCP/WebSocket connection pooling, Linux epoll file descriptor limits, Redis Pub/Sub session affinity routing, and Cassandra time-series commit logs.
Socket Memory & Epoll Capacity Math
Why naive HTTP REST polling melts infrastructure at 500k concurrent users, and how operating systems budget RAM and file descriptors for long-lived WebSockets.
Short Polling HTTP Breakdown
If 500,000 active mobile clients poll for new messages once every 2 seconds, the gateway cluster receives an unsustainable request barrage:
Linux Kernel TCP Overhead
Unlike stateless HTTP, each WebSocket holds a permanent TCP socket descriptor and kernel buffers in gateway memory:
Epoll & File Descriptors
Default Linux distributions cap process file descriptors at 1,024, immediately rejecting connection 1,025 with EMFILE: Too many open files.
Session Affinity & Inter-Gateway Pub/Sub
Alice is connected to Gateway Pod 02. Bob is connected to Gateway Pod 09. How does Gateway Pod 02 route a message to Bob in sub-millisecond time without broadcasting to every server?
Distributed Session Registry (Redis)
When a client connects and completes TLS negotiation, the gateway writes an ephemeral session record into Redis Cluster:
Clients emit heartbeat pings every 45s to extend the 90s TTL. If a gateway node dies without sending a disconnect event, stale sessions expire naturally within 90 seconds.
Inter-Gateway Message Bus (Redis Pub/Sub)
Each gateway pod subscribes to its own unique topic channel: channel:gw-pod-09.
- Alice sends message destined for Bob to Gateway 02.
- Gateway 02 checks Redis: Bob is on
gw-pod-09. - Gateway 02 publishes payload to
channel:gw-pod-09. - Gateway 09 receives event, finds Bob's local socket descriptor, and flushes frame.
If Bob has no active Redis key, Gateway 02 immediately drops an event onto RabbitMQ to trigger Apple APNs / Google FCM mobile push alerts.
The Reconnect Thundering Herd Storm
What happens when a cell tower blip drops 50,000 mobile devices simultaneously, and all 50,000 attempt to reconnect within 1 second?
The TLS Cryptographic CPU Spike
Maintaining an established TCP connection costs near-zero CPU. However, establishing a TLS 1.3 connection requires Diffie-Hellman asymmetric key exchange (ECDHE curve computation) and RSA signature verification.
The Cascade Death Spiral
- Event Loop Lockup: Gateway CPU reaches 100% saturation. Event loop lag surges from 2ms to over 4,000ms.
- Liveness Probe Failure: Kubernetes kubelet checks
GET /healthz. The blocked thread cannot respond within 3 seconds. - Pod Termination: Kubernetes kills 4 of the 10 gateway pods for failing health checks.
- Cascading Pod Collapse: Remaining 6 pods instantly absorb the entire 50k handshake queue, freeze, and are killed in turn. The cluster enters an endless CrashLoopBackOff.
The Resilient Production Architecture
How StackMonkey models the fault-tolerant Chat Service topology with Layer 4 load balancing, session affinity registries, Redis Pub/Sub, and ScyllaDB time-series storage.
Simulate the Chat Service Topology Live
Stress test 500k concurrent WebSocket sessions, inject network disconnect waves to simulate thundering herd reconnects, and verify queue depth thresholds live in your browser.