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

Parents Reply Children
  • Hi Charlie, that's okay.

    Yes I saw that endpoint, but it says it's deprecated and should not be used. The suggested endpoint to use instead https://api.nrfcloud.com/v1#tag/Account-Devices/operation/CreateAccountDevice - specifically says:

    "When a new account device (and subsequent certificate) is created, both the public AND private keys are returned. If an account device already exists, only the public key is returned. If the private key is lost, the certificate should be rotated to obtain a new public/private key pair".

    This conforms with the result of my attempt trying it, i.e. the second time I call the endpoint I get the same clientId back as well as the public key. Limiting me to only have one of these account devices created. So, there is no way for me to connect to the NRF MQTT broker with different device ID:s (client ID:s) since those are tied to a account device, which I can only create one of.

    Is this by design, or?

    Best regards,

    Rasmus

    EDIT: I did try the deprecated endpoint as well that you provided, that does not create a new account device either if I already have one. It only returns the same information of the one already created.

Related