Search code examples
javaduplicatespublish-subscribe

How can duplicate processing of messages be avoided if two instances of same app subscribes to a topic?


I have two instances of same application running and they both subscribe to same topic. Messages are going to to published to both servers and both servers are going to process that same message. How can duplicate processing of messages be avoided if two instances of same app subscribes to a (same)topic using a stateless approach?


Solution

  • Topic has exactly opposite purpose from what you try to achieve. Queue stands for it.

    If you still want to use topic you must have only one subscriber/listener on top of your application instances.

    Then you have to force on your own any load balance algorithm between your application instances (e.g. round-robin or something more complex).