Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatFlow - Distributed Real-Time Chat System

A high-performance, scalable WebSocket-based chat system built with Java, RabbitMQ, and AWS. Designed to handle high-throughput concurrent messaging across multiple server instances with horizontal scaling capabilities.

Note: This project was developed as part of a distributed systems course project, demonstrating real-world scalability patterns and production-ready architecture.

Message Flow Diagram

Features

  • Real-time Messaging: WebSocket-based bidirectional communication for instant message delivery
  • Horizontal Scaling: Load-balanced architecture supporting multiple server instances via AWS Application Load Balancer
  • Message Queue Integration: RabbitMQ for reliable message distribution and decoupling
  • Room-based Chat: Support for multiple chat rooms with isolated message broadcasting
  • High Throughput: Optimized to handle 4,000+ messages per second
  • Connection Pooling: Efficient resource management with channel pooling and thread pools
  • Health Monitoring: Built-in health check endpoints for load balancer integration
  • Message Deduplication: Prevents duplicate message delivery across distributed instances

Architecture

System Components

        ┌─────────────┐
        │   Client    │
        │ (WebSocket) │
        └──────┬──────┘
               │
               ▼
┌────────────────────────────────┐
│      Chat Server Instance      │
│  ┌──────────┐  ┌─────────────┐ │
│  │WebSocket │  │Message Pub. │ │
│  │  Server  │  │  (Pool:100) │ │
│  └────┬─────┘  └──────┬──────┘ │
│       │               │        │
│  ┌────▼───────────────▼──┐     │
│  │   RabbitMQ Exchange   │     │
│  └────────────┬──────────┘     │
└──────────────-┼────────────────┘
                │
                ▼
┌─────────────────────────────────┐
│         RabbitMQ Broker         │
│  ┌──────────────────────────┐   │
│  │  (room.1 - room.20)      │   │
│  └──────────┬───────────────┘   │
└─────────────┼───────────────────┘
              │
              ▼
┌─────────────────────────────────┐
│      Chat Server Instance       │
│  ┌─────────────┐  ┌──────────┐  │
│  │Message Cons.│  │Broadcast │  │
│  │(Pool:40)    │─►│  Engine  │  │
│  └─────────────┘  └────┬─────┘  │
└────────────────────────┼────────┘
                         │
                         ▼
                    ┌─────────────┐
                    │   Clients   │
                    │ (WebSocket) │
                    └─────────────┘

Message Flow

  1. Client Connection: Client connects via WebSocket to /chat/{roomId}
  2. Message Publishing: Server receives message and publishes to RabbitMQ topic exchange
  3. Queue Distribution: RabbitMQ routes message to room-specific queue
  4. Message Consumption: All server instances consume from their respective queues
  5. Broadcasting: Each server broadcasts to connected clients in that room
  6. Deduplication: Message IDs prevent duplicate delivery

Key Design Decisions

  • Thread Pool Architecture: Separate pools for message processing (100), broadcasting (20), and consuming (40)
  • Channel Pooling: 100-channel pool for RabbitMQ publishing to minimize connection overhead
  • Prefetch Optimization: QOS prefetch of 20 messages per consumer for balanced throughput
  • Queue TTL: 5-minute message TTL with max length of 50k messages per queue

Tech Stack

  • Language: Java 11
  • WebSocket: Java-WebSocket 1.5.4
  • Message Broker: RabbitMQ 5.18.0
  • JSON Processing: Gson 2.10.1
  • Build Tool: Maven 3.x
  • Cloud Infrastructure: AWS EC2, Application Load Balancer
  • Monitoring: RabbitMQ Management Console, Custom metrics

Installation

Prerequisites

  • Java 11 or higher
  • Maven 3.6+
  • RabbitMQ Server (3.8+)

Build

# Build server
cd server-v2
mvn clean package

# Build client
cd ../client-v2
mvn clean package

The build process creates JAR files with all dependencies:

  • server-v2/target/chatflow-server-1.0-jar-with-dependencies.jar
  • client-v2/target/chatflow-client-1.0-jar-with-dependencies.jar

Deployment

RabbitMQ Setup

# On RabbitMQ EC2 instance
cd server-v2/src/main/java/io/chatflow/deployment
./setup-rabbitmq.sh

# Access management console at http://<rabbitmq-ip>:15672
# Default credentials: admin/admin

Server Deployment

# Copy JAR to server instances
scp -i ~/.ssh/key.pem \
  target/chatflow-server-1.0-jar-with-dependencies.jar \
  ec2-user@<ec2-ip>:~/

# SSH and start server
ssh -i ~/.ssh/key.pem ec2-user@<ec2-ip>
export RABBIT_HOST=<rabbitmq-private-ip>
java -cp chatflow-server-1.0-jar-with-dependencies.jar io.chatflow.server.ChatServer

Run Client

# Single instance test
java -cp target/chatflow-client-1.0-jar-with-dependencies.jar \
  io.chatflow.client.ChatClient ws://<server-ip>:9090/chat/

# Load balanced test
java -cp target/chatflow-client-1.0-jar-with-dependencies.jar \
  io.chatflow.client.ChatClient ws://<alb-dns>/chat/

Port Configuration

  • WebSocket Server: 9090
  • Health Check: 8081
  • RabbitMQ AMQP: 5672
  • RabbitMQ Management: 15672

Configuration

Optimal Server Configuration

# Thread Pool Sizes
message.processor.threads=100
consumer.threads=40
broadcast.threads=20

# RabbitMQ Settings
channel.pool.size=100
prefetch.count=20

# Queue Settings
queue.max.length=50000
message.ttl=300000  # 5 minutes

Load Balancer Setup

  • Type: Application Load Balancer (ALB)
  • Target Group: 2-4 EC2 instances
  • Health Check: /health endpoint on port 8081
  • Stickiness: Disabled (for distributed testing)
  • Algorithm: Round-robin

Performance Metrics

Test Results

Configuration Throughput Success Rate Avg Queue Depth
1 Instance 5,571 msg/s 99.94% < 300
2 Instances 5,725 msg/s 99.97% < 300
4 Instances 4,402 msg/s 99.95% < 300

Load Test Configuration

  • Total Messages: 500,000
  • Client Threads: 128
  • Number of Rooms: 20
  • Message Types: 90% TEXT, 5% JOIN, 5% LEAVE

Key Findings

  • Consistent throughput across scaling configurations
  • Queue depths remain stable (< 1000 messages)
  • High success rate (> 99.9%)
  • No message loss or duplicate delivery
  • Efficient resource utilization

See monitoring/README.md for detailed performance analysis and screenshots.

Project Structure

.
├── server-v2/
│   ├── src/main/java/io/chatflow/
│   │   ├── server/
│   │   │   ├── ChatServer.java          # Main WebSocket server
│   │   │   ├── RoomManager.java         # Room management
│   │   │   ├── WebSocketBroadcaster.java # Message broadcasting
│   │   │   ├── HealthServer.java        # Health check endpoint
│   │   │   └── Metrics.java             # Performance metrics
│   │   ├── rabbitmq/
│   │   │   ├── MessagePublisher.java    # RabbitMQ publisher with pooling
│   │   │   └── MessageConsumer.java     # RabbitMQ consumer with deduplication
│   │   ├── deployment/
│   │   │   ├── setup-rabbitmq.sh        # RabbitMQ setup script
│   │   │   └── start-server.sh          # Server startup script
│   │   └── monitoring/
│   │       └── README.md                 # Performance analysis
│   └── pom.xml
├── client-v2/
│   ├── src/main/java/io/chatflow/
│   │   ├── client/
│   │   │   ├── ChatClient.java          # Load test client
│   │   │   ├── SenderThread.java        # WebSocket sender
│   │   │   ├── MessageGenerator.java    # Message generation
│   │   │   └── Metrics.java             # Client metrics
│   │   └── model/
│   │       └── ChatMessage.java         # Message model
│   └── pom.xml
└── README.md

🔍 Monitoring

Health Check Endpoint

curl http://localhost:8081/health

Response:

{
  "status": "UP",
  "timestamp": "2024-01-15T10:30:00Z"
}

RabbitMQ Monitoring

Access the RabbitMQ Management Console at http://<rabbitmq-ip>:15672 to monitor:

  • Queue depths and rates
  • Message throughput
  • Connection status
  • Consumer utilization

Testing

Load Testing

The included client can generate high-volume load tests:

java -cp chatflow-client-1.0-jar-with-dependencies.jar \
  io.chatflow.client.ChatClient ws://<server-url>/chat/

The client will:

  • Generate 500,000 messages
  • Distribute across 20 rooms
  • Use 128 concurrent threads
  • Report throughput and success rates

Message Format

{
  "userId": "12345",
  "username": "user12345",
  "message": "Hello, world!",
  "timestamp": "2024-01-15T10:30:00Z",
  "messageType": "TEXT"
}

Key Achievements

  • Built a distributed chat system capable of handling 4,000+ messages/second
  • Implemented efficient connection pooling and thread management
  • Achieved 99.9%+ success rate under high load
  • Designed scalable architecture with horizontal scaling support
  • Integrated RabbitMQ for reliable message distribution
  • Optimized queue management and message deduplication

License

This project is available for portfolio and educational purposes.

Author

Sahilpreet Aneja

Developed as a demonstration of distributed systems architecture, real-time messaging, and cloud deployment expertise.


Note: This project demonstrates production-ready patterns for distributed systems, real-time communication, and scalable architecture design.

About

A high-performance, scalable WebSocket-based chat system built with Java, RabbitMQ, and AWS

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages