Suppose I have an IoT device that I am going to monitor (say, turn on / off) and monitor (for example, collect temperature readings). It seems MQTT may be appropriate. I could publish messages on the device to control it, and the device could publish messages to the broker to report temperature readings. So far so good.
Problems arise when I try to create an API for device management.
The day is coming when the device subscribes to two questions:
- / identifier device / control / on
- / identifier device / control / off
Then I post on these topics in some order. But given the fact that messaging is usually an asynchronous process, there are no guarantees regarding the order of messages received by the device.
So, if two posts are published in the following order:
- / identifier device / control / on
- / identifier device / control / off
they can be taken in reverse order, leaving the device turned on, which can have dramatic consequences depending on the context.
Of course, the API could be designed in some other way, for example, there can only be one topic
- / device identifier / management
and the payload of the individual messages will be the value of the individual message (on / off). Therefore, if you publish messages in this section in a certain order, they are expected to be received in the same order on the device.
But what if the publication order of individual topics cannot be guaranteed? Assume the following system architecture for IoT devices:
/ control service \ application -> broker -> control service -> broker -> IoT device \ control service /
System Components:
- an application that effectively controls the device by publishing messages to the broker
- typical message broker
- management service with some business logic
The important role is that, as in most modern distributed systems, the management service is a distributed multi-instance object that can simultaneously process several management applications from an application. Therefore, the order of messages published by the application can be completely mixed when delivered to the IoT device.
Now, given the fact that most MQTT brokerage companies sell only QoS0 and QoS1, but QoS2 it becomes even more interesting, since such control messages can be delivered several times (provided that QoS1 - see https://stackoverflow.com/a/3129648/) / ... ).
My point is that separate topics for control messages are a bad idea. The same goes for one topic. In both cases, there is no guarantee of message delivery.
The only solution to this particular problem that comes to my mind is message versioning, so old (obsolete) messages can simply be skipped when delivered after another message with a more recent version property.
- Did I miss something?
- Is the post version the only solution to this problem?