Skip to content

ChetanBhosale/scalable_socket_pub_sub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Scalable Chat System Using Redis Pub/Sub Model

A scalable chat system ensures that messages sent by users are efficiently distributed across multiple WebSocket servers, enabling high availability and performance. Redis Pub/Sub (Publish-Subscribe) plays a key role in synchronizing messages across different instances in a distributed architecture.


📌 Architecture Overview

1️⃣ WebSocket Layer (Multiple Workers)

  • The chat server runs multiple WebSocket server instances using Node.js Cluster Mode to utilize multiple CPU cores.
  • Each worker process independently manages WebSocket connections and handles incoming messages.
  • Clients can connect to any WebSocket instance, meaning messages need to be shared across all instances.

2️⃣ Redis Pub/Sub Layer

  • Redis acts as a real-time message broker.
  • When a WebSocket instance receives a message, it publishes the message to a Redis channel (SCALE_CHAT).
  • All WebSocket instances subscribe to the SCALE_CHAT channel to receive messages, ensuring every client gets the message regardless of which server they are connected to.

3️⃣ Message Synchronization

  • When a user sends a message, the WebSocket server publishes it to Redis.
  • Redis then broadcasts the message to all subscribed WebSocket instances.
  • Each WebSocket instance forwards the message to connected clients, ensuring real-time message delivery.

🔄 Message Flow

1️⃣ Client sends a message → WebSocket server receives it.
2️⃣ Server publishes the message to Redis (SCALE_CHAT).
3️⃣ All servers receive the message from Redis.
4️⃣ Servers broadcast the message to connected clients.


⚡ Benefits of Redis Pub/Sub for Scaling

Load Balancing

  • Clients are distributed across multiple WebSocket servers.
  • Message delivery is synchronized using Redis, ensuring no duplication or loss.

Fault Tolerance

  • If a WebSocket server crashes, other servers continue handling connections.
  • Redis ensures messages are not tied to a single server.

Horizontal Scalability

  • More WebSocket instances can be added to handle increased load.
  • Redis Pub/Sub keeps all instances in sync without complex inter-server communication.

🚀 Example Use Cases

  • Live Chat Applications (e.g., WhatsApp, Slack)
  • Stock Market Tickers (Real-time price updates)
  • Gaming Chat Systems (Real-time multiplayer communication)
  • Collaborative Editing Tools (Google Docs-style live updates)

This architecture ensures real-time performance, reliability, and scalability while being lightweight and easy to implement! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published