Skip to content

About

Real-time chat backend designed for write throughput. Decoupled publish, broadcast, and persistence paths sustain 8K+ msg/s end to end. Java 17, Spring Boot, RabbitMQ, Cassandra, Redis, AWS.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatFlow

A distributed real-time chat backend. Clients open WebSocket connections, messages are validated and published to RabbitMQ, server-side subscribers fan them out to connected sessions in the same room, and a consumer persists everything to Cassandra via a write-behind batching layer. A Redis-cached REST API serves analytics queries over the stored history.

Architecture

                    Clients (WebSocket)
                           |
                  AWS Application Load Balancer
                           |
        +----------------------------------------+
        |          server  (N instances)         |
        |  validate + publish + ACK              |
        |  subscribe + broadcast                 |
        |  metrics REST API                      |
        +----------------------------------------+
                  |                  |
              RabbitMQ            Redis
        topic exchange         read cache
        per-room queues        for /api/metrics
                  |
              consumer
        pull + write-behind
                  |
              Cassandra
        denormalized tables
        per query pattern
Component Port Role
server 8080 WebSocket endpoint, RabbitMQ publisher, broadcaster, metrics API
consumer 8081 RabbitMQ consumer with batched Cassandra writes
client (driver) Multi-threaded WebSocket load generator
RabbitMQ 5672 Topic exchange chat.exchange, queues room.1 through room.20
Cassandra 9042 6 tables, each shaped around a single read pattern
Redis 6379 Read-through cache for the analytics endpoints

Message flow

A client connects to wss://{lb}/chat/{roomId}. The server validates the inbound JSON (user id, username, body length, timestamp, type), wraps it in a QueueMessage envelope with a generated id, publishes to room.{roomId} on the topic exchange, and ACKs the client. Every server instance bound to that room queue receives a copy and broadcasts it to its locally connected sessions through SessionRegistry.

In parallel the consumer pulls from the same queues, hands the message to the processing pipeline, and enqueues it into an in-memory write-behind buffer. A pool of writer threads drains the buffer, batching by size or by time interval, and fires per-table async writes against Cassandra. The RabbitMQ ACK happens at enqueue time, which keeps consumption decoupled from database latency.

The read path is plain Spring caching: the controller dispatches to a service method annotated with @Cacheable, Redis is checked first, and on a miss the prepared CQL statement runs against Cassandra and the result is stored back in Redis with a short TTL.

Quick start

Prerequisites: Java 17, Maven 3.8, RabbitMQ, Cassandra 5, Redis.

cd server   && mvn package -DskipTests && cd ..
cd consumer && mvn package -DskipTests && cd ..
cd client   && mvn package -DskipTests && cd ..

Run locally (with brokers and Cassandra already up):

java -jar server/target/chatflow-server-0.0.1-SNAPSHOT.jar
java -jar consumer/target/chatflow-consumer-0.0.1-SNAPSHOT.jar
java -jar client/target/chatflow-client-0.0.1-SNAPSHOT.jar localhost 8080

For an AWS deployment see docs/DEPLOYMENT.md. The scripts in scripts/ provision RabbitMQ, Cassandra, Redis, the ALB, and all application instances.

Repository layout

server/      Spring Boot WebSocket server, publisher, subscriber, metrics API
consumer/    RabbitMQ consumer with Cassandra write-behind persistence
client/      Multi-threaded WebSocket load test client
database/    Cassandra schema and bootstrap script
scripts/     AWS provisioning, deployment, and load test automation
jmeter/      Read-path benchmarks
docs/        Architecture notes and deployment guide

Tech stack

Java 17, Spring Boot 3.3, RabbitMQ (AMQP 0.9.1, topic exchange), Apache Cassandra 5.0 with the DataStax Java driver 4.17, Redis 6 via Spring Data Redis and Spring Cache, AWS EC2 with an Application Load Balancer.

Design notes

Cassandra was picked for write throughput. The denormalized layout costs six to seven writes per message, but Cassandra absorbs that comfortably, each read path stays single-partition, and primary-key upserts give us idempotency without any explicit dedupe.

The broadcast path runs on the server itself rather than as a separate service. Each server instance binds to the room queues it cares about and pushes messages to its locally connected sessions. This removes a network hop, eliminates service discovery between the broadcaster and the WebSocket layer, and keeps the fan-out close to where the sessions live.

The consumer is intentionally write-behind. RabbitMQ ACKs happen as soon as the message is in the in-memory queue, so the consume rate is not coupled to Cassandra latency. The queue absorbs transient slowdowns; the flush size and interval bound how much in-flight work the system holds at any moment.

The metrics API is read-heavy and the answers change slowly, so a Redis cache between the service and Cassandra is a good fit. Spring's @Cacheable handles the lookup, miss, and refill; Cassandra only sees one request per cache key per TTL window.

About

Real-time chat backend designed for write throughput. Decoupled publish, broadcast, and persistence paths sustain 8K+ msg/s end to end. Java 17, Spring Boot, RabbitMQ, Cassandra, Redis, AWS.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages