Message Bus (Live RabbitMQ & WebSocket Broadcast Engine)

  • Backend
  • Realtime
  • Message Bus
  • DevOps

Production · NestJS, Node.js, RabbitMQ, WebSockets …

Executive Overview

A high-throughput live broadcasting microservice built with NestJS, RabbitMQ, and WebSockets that consumes rapid odds, scores, and match events from upstream message brokers and broadcasts real-time updates to connected clients.
The Challenge & Bottleneck

Core Problem

Broadcasting high-frequency updates (live sports scores and fluctuating betting odds) to thousands of concurrent mobile and web clients cannot be handled by database polling. Direct client connections to the primary ingestion databases cause socket and connection starvation, while unbuffered broadcast bursts can desynchronize clients during game scoring events.

Engineering Approach

Architectural Solution

Engineered a dedicated message bus service that decouples event ingestion from client delivery. The service listens to topic exchanges on a self-hosted RabbitMQ cluster and fans out serialized binary/JSON frames to connected WebSocket clients with socket room isolation, heartbeat monitoring, and Cronitor health checks.

Quantified Outcomes

Measurable Impact

Delivered sub-15ms end-to-end event propagation from RabbitMQ to client devices, supported thousands of concurrent active socket subscriptions with zero message drops, and eliminated 100% of read traffic on underlying transactional databases for live updates.

System Architecture

Component topology, protocol boundaries, and data flow.

Message Bus (Live RabbitMQ & WebSocket Broadcast Engine) System Topology
Architecture Flow
CLIENT CONSUMERWeb & API CallsHTTPS / REST PayloadsJSON Schema InputBOUNDARY GATEWAYNginx / Reverse ProxyTLS TerminationRate Limiting & AuthNSERVICE CORE LOGIC• Domain Services & Controllers• DTO Runtime Validation• AWS Secrets Manager Config• Health Readiness ProbesPERSISTENCEPostgreSQL / RedisACID TransactionsDocker / EKS Hosted

Reliability & Production Security

Integrated New Relic APM agent for distributed transaction tracing, Cronitor ping monitors for uptime alerting, dead-letter exchanges in RabbitMQ for unroutable frames, and graceful socket buffer draining upon container SIGTERM.

Deployment & Infrastructure

Multi-stage Dockerized container deployed on AWS with auto-scaling container tasks and sticky session routing behind Application Load Balancers.
Engineering Post-Mortem & Insights

What I Learned

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

1

Decouple Ingestion from Broadcast via Message Brokers

Attempting to push WebSocket frames directly from the data ingestion service creates severe backpressure when client connections spike. Buffering events in RabbitMQ topic exchanges allows consumer nodes to scale independently based on connected client counts.

2

Pre-Serialize Broadcast Payloads Once for All Clients

Serializing the same JSON payload 5,000 times for 5,000 connected sockets burns CPU cycles unnecessarily. Serializing the message payload once into a shared buffer and dispatching the raw buffer across socket descriptors reduces CPU utilization by 70%.

3

RabbitMQ Prefetch Limits Prevent Memory Exhaustion

Default RabbitMQ consumers accept unlimited unacknowledged messages, causing worker memory to balloon during high-volume sports match events. Setting an explicit prefetch count (e.g. 50-100) ensures worker nodes process batches safely within memory budgets.

4

Active Heartbeats Detect Zombie Sockets

Mobile clients on cellular networks frequently disconnect without sending TCP FIN packets, leaving zombie sockets lingering in memory. Implementing 30-second ping/pong heartbeats ensures ghost connections are terminated and socket file descriptors reclaimed promptly.

Future Roadmap & Architectural Evolution

  • →Implement selective client delta-patching to broadcast only changed record attributes rather than entire game objects.
  • →Benchmark MQTT protocol alongside WebSockets for low-bandwidth mobile environments.
Message Bus (Live RabbitMQ & WebSocket Broadcast Engine) | Siddhant Ghosh