Multi instance backend subscribing to MQTT topic

Hi, I am wondering about the best way to handle the case where my backend is subscribing to MQTT topics in NFR cloud, when I am running multiple instances of it, for example in Kubernetes.

The requirement is that one of the instances should get one message, not all - in a load balanced fashion.

I have read about shared subscriptions and tried to use it. Seems to be working, however I am facing an issue where all of my instances keeps disconnecting and reconnecting. I suspect this is because the MQTT broker only allows one connection per client id, and I am using the same for all my instances (btw I am using a device account to subscribe to the topics from my backend).

My initial thoughts for a solution is:

  1. Some form of leader election mechanism, to give one of my instances a "leader" role, hence subscribing to the topics and the other instances are not (works but complicated)
  2. Generating as many device accounts as I plan to have replicas and use different device account per replica (I don't particularly like this because I don't want to create another account when scaling up, and the logic for grabbing an account feels non trivial)

Any other thoughts for a solution, that might be more straight forward?

Best regards,

Rasmus

Related