title: Routing messages from Kafka to web socket clients connected to application server cluster
body: <p>I would like to figure out the best way to route messages from Kafka to web socket clients connected to a load balanced application server cluster. I understand that spring-kafka facilitates consuming and publishing messages to a kafka topic, but how does this work in a load balanced application server scenario when connecting to a distributed kafka topic. Here are the requirements that I would like to satisfy, with the overall goal of facilitating peer to peer messaging in an application with a very, very large volume of users:</p>
  
  <ol>
  <li>Web clients can connect to a tomcat application server via web sockets connection via a load balancer.</li>
  <li>Web client can send a message/notification to another client thats connected to different tomcat application server.</li>
  <li>Messages are saved in the database and published to a kafka topic/partition that can be consumed by the appropriate web clients/users.</li>
  <li>Kafka can be scaled to many brokers with many consumers.</li>
  </ol>
  
  <p>I can see how this can be implemented quite easily in a single application server <a href="https://github.com/Seunghoon-Oh/chatting-kafka" rel="noreferrer">scenario</a> where the consumer consumes all messages from a kafka topic and re-distributes via spring messaging/websockets. But I can't figure out how this would work in a load balanced application server scenario where there are consumers on each application server forming an overall consumer group for the kafka topic. Assuming that each of the application servers are are consuming sub-sets/partitions of the kafka topic, how do they know which server their intended recipients are connected to? And even if they knew which server their recipients were connected to, how would they route the message to them via websockets?</p>
  
  <p>I considered that the application server load balancing could work by logging users with a particular routing key (users starts with 'A' etc) on to a specific application server, then only consuming messages for users starts with 'A' on that application server. But this seems like it would be difficult to maintain and would make autoscaling very difficult. This seems like it should be an common scenario to implement but I can't find any tools or approaches that fit this scenario.</p>