mac07
mac07

Reputation: 61

WebSocket with Kafka/Active Mq/Rabbit Mq | Spring

My On-Prem application details:

Frontend - Reactjs deployed in 4 VMs [a,b,c,d]
Backend - Spring microservices[Stateless] deployed in 4 VMs [w,x,y,z]]
Database - Sql server
Load Balancer - F5
CDN - Akamai
Request flow: user's browser --> Akamai,F5 --> UI server[anyone from 4 instances: Round-Robin] --> F5 --> API server[anyone from 4 instances: Round-Robin]

Requirement: When an admin creates a notification from our application's UI, I want all users connected to our application to see the notification in real-time on the Homepage. If they log in later they can go to the Nofification page where we are showing all the nofications that are fetched from db.

Currently, we are showing the notification on the Homepage and Notification page but this is not real-time.

I am thinking of following this approach which uses Kafka :
When an admin creates a notification, send the notification to a Kafka topic "notify" after persisting in a db table. Add a Kafka listener method in the microservice to get the notification from "notify" topic and send to UI using Websocket. The consumer group will be different for the 4 instances so that every instance read from the same partition. But I do not see any STOMP related info on Kafka documentation pages like for other brokers to hook up Kafka with Websocket.

The alternatives are ActiveMQ and RabbitMQ.

  1. What are the pros and cons of the Kafka approach here?
  2. What do you think about using ActiveMQ / RabbitMQ?

Note: We are planning to migrate to Google Cloud in a few months. So my solution should be GCP compatible.

Upvotes: 0

Views: 54

Answers (1)

Evandro Maddes
Evandro Maddes

Reputation: 41

In my current architecture, I use RabbitMQ as the message broker. I chose RabbitMQ over other alternatives because it’s already integrated into the system and provides a simple solution for handling WebSocket sessions beyond the in-memory sessions offered by spring websocket library.

A key feature of RabbitMQ is its ability to share user sessions across multiple servers connected to the same RabbitMQ broker. This is crucial for scenarios where users are connected to different servers in a distributed system.

For example, if an admin is connected to Server A via a WebSocket and sends a notification, RabbitMQ ensures that all users connected to the application—regardless of whether their websocket connection is open on Server A or on another server—can receive the notification. This is because RabbitMQ shares user session from and to your application's servers.

To achieve this, the admin publishes the notification to a specific queue (e.g., "notify-queue"). All users subscribed to this queue, no matter which server they are connected to, will receive the message.

If you're considering Kafka as an alternative to RabbitMQ, you should evaluate whether Kafka provides a similar mechanism for distributing messages to WebSocket clients in a multi-server architecture.

Resources:

Upvotes: 1

Related Questions