Scenario
I have Two Spring Boot services(service-a,service-b)
and one front-end service
. The front end service
will connect to my service-a
via STOMP Web Socket broadcast channel. When messages arrive,service-a
publishes to the Kafka topic, and service-b
handles them depending on the topic and sends them back to service-a
via another kafka topic. service-a
will listen to the topic and deliver it back to the front-end service via websocket.
Diagram
I'll show you a sample flow of my service communication, which is totally functional.
Sample Microservice Flow
Problem
So, service-a
is mainly communicate with mobile-service
. And I'll run two instances for service-a
and two mobile-service will connected to different instances of service-a
. I mean one of the mobile-service is connected to service-a-1
and another is to service-a-2
but both are connected to same broadcast channel. When service-b
publish a message to topic, one of the service-a
instance will listen to that topic and send to mobile. However that instance which is listen the topic have only one websocket session that was register and only that session will get that message and the one who register to another instance with same channel won't get that message. I'll show a diagram below for understanding the issue.
Problem of the flow
Solution that might work
Websocket And Redis Session
Websocket is statefull so, I think I have to store on redis or somewhere but how, the whole WebSocketSession Object into redis?
Apache Kafka fanout
Making Kafka consumer group Id different for same prefix with different number for same service instances (service-a-{random number})
. With this kafka will listen that topic from all instance so it can send to all mobile-service.