J2EE/Quarkus/Undertow - Handle websocket sessions between replicas

6/8/2020

I'm facing a problem on Quarkus (with Undertow) but I think this problem can be the same in all J2EE applications :

  • I have an application Scaled on Kubernetes (Exemple I have 3 pods of the same service).
  • I have an application scoped bean (WebsocketService) that store websocket sessions in a property (ConcurrentHashMap).

  • On the one hand A user (User1) initiate a websocket session on the service (so kubernetes redirect it to one of the 3 pods of the service). The websocket session is binded to this pod (Pod1) and WebsocketService store the sessions in the property.

  • On the other hand another user (User2) send a Request to this service : kubernetes doesn't choose the same pod (call it Pod2). That request trigger a method of WebsocketService which emit on the websockets a message.

  • WebsocketService has a property that store the websockets sessions.

My problem is :

  • The request of User2 doesn't go the same pod of User1, so, the websocket session is not stored in the property of the application scoped bean.
  • My User1 doesn't get the message trough the websocket beacause Pod1 has an instance of Webservice different of Pod2.

How can I solve this easily.

I have some ways :

  • Create a websocket application independant with only one replica
  • Use kafka to propagate on all pods the message to send through the websocket.
  • Can I store a variable in a Redis Database (or anything else) And get it on each pod ?

What can't be done :

  • Stateful Pods : Because User1 can be binded to Pod1 and User2 to Pod2 anyway.
  • Only one replica of my service because there are a lot of people, and it can be scalable.

Thank you in advance,

SOLUTION

  • I put the userId in the session properties.
  • I'm using my kafka cluster to make all my pods of the service receive the message to send through the websocket, each pods loop over there websocket sessions and if they find the session with the good userId it send the message !
-- Dorian Maliszewski
jakarta-ee
kubernetes
quarkus
undertow
websocket

0 Answers