Clustering: Client state
Ensure that MQTT clients that connect by specifying cleanSession=False
always connect to an assigned server in the cluster.
The MQTT protocol allows clients to connect in one of two cleanSession
modes. If you specify cleanSession=True
, the subscriptions, and other state data that is kept while the client is connected, are deleted when the client disconnects. If you specify cleanSession=False
, the subscriptions, and other state data that is kept while the client is connected, are persisted when the client disconnects and resumed when the client reconnects; however, the data is persisted only on the server to which the client was connected and is not sent to other servers in the cluster.
If you need a client that was connected with a setting of cleanSession=False
to resume the state that it left after it disconnected, ensure that the client reconnects to the same server in the cluster. The client can then receive messages on subscriptions that were published while it was disconnected and resolve the state of messages that it published.
cleanSession=False
always connect to an assigned server in the cluster. The assignment of such a server in the cluster can be achieved either if each client remembers which server it must connect to, or by the use of an intelligent load balancer. The load balancer can direct clients that are specified cleanSession=True
to any server in the cluster, and direct clients that are specified cleanSession=False
to the same server perhaps by using the specified client identifier to determine which server is the client's assigned server. cleanSession=False
subsequently reconnects with a setting of cleanSession=True
, the first time that it reconnects, the load balancer should connect the client to the server that it was assigned to when it was specified with cleanSession=False
. The load balancer can subsequently direct the client to any server in the cluster.