MQTT – How to consume a snapshot value published in the past.

Introduction

MQTT, or Message Queuing Telemetry Transport, is a lightweight messaging protocol designed for devices with limited processing power and bandwidth. To understand MQTT, it’s essential to grasp its fundamental principles, such as the publish/subscribe messaging pattern and Quality of Service levels. For beginners looking to delve into MQTT, resources like the official MQTT website (mqtt.org), the Eclipse Mosquitto broker, and online tutorials on platforms like YouTube and Udemy can be incredibly helpful in getting started.

In this post, I’ll explore design considerations for transporting time series data typically generated by analog sensors and distributed to various subsystems such as displays, databases, supervision systems, cloud systems for machine learning, predictive maintenance, and more.

Use Case 1 -Monitor and share the temperature inside a freezer.

In this configuration, a temperature sensor has been included in a freezer. A component is now in place to change the information gathered by the sensor into an mqtt topic, structured like this:

Topic: freezer/<guid>/temperature

Payload: {unix_time_stamp:<ts> , value:<int> }

The layout of the component design is illustrated in the image provided:

PlantUML Syntax:<br />
skinparam BackgroundColor transparent<br />
skinparam componentStyle rectangle<br />
component “temperature sensor” as a<br />
component “publisher” as b<br />
component “mqtt broker” as c<br />
component “Display” as d<br />
component “Local BMS” as e<br />
component “Cloud Client” as f</p>
<p>[a]-up->[b] : read<br />
[b]->[c] : publish<br />
[c]<-up->[d] : subscribe<br />
[c]<-right->[e] : subscribe<br />
[c]<-down->[f] : subscribe<br />

Consider this scenario: what if the section that displays the temperature information is refreshed every minute? If users refresh the page immediately after receiving the latest update, they will have to wait approximately a minute to receive new data. While this delay may be suitable in some cases, it may not be ideal for all situations. In the next part, we will investigate several alternatives to address this issue.

Use Case 2 – Extend use case 1 to display the current temperature

The use case 1 as a small draw back, the update of the subscribes depend on the publish event.

If the publishing period is set to 2 minutes, restarting the display subscriber could result in a delay of up to 2 minutes for updates. To prevent this, consider adding 1 or 2 extra topics depending on the expected data updates. One topic can be used to request current data, which the temperature can subscribe to. Then, a response topic can be published or the existing one can simply be updated. If a response is published, the requester needs to subscribe to it. Overall, this approach increases design complexity and necessitates careful attention to prevent errors.

Let’s design this 2 options:

Use case 2 – Option 1 – Force publisher

Is this case we can design this topic:

Topic: freezer/<guid>/temperature.request

Payload: None

PlantUML Syntax:<br />
 hide footbox<br />
 !pragma teoz true</p>
<p> participant “Temperature Publisher” as pub<br />
 participant “MQTT Broker” as MQTT<br />
 participant “Display” as Display<br />
 participant “Local BMS” as BMS<br />
 participant “Cloud Client” as Cloud</p>
<p> == startup ==<br />
 pub –> MQTT : subscribe to temperature.request<br />
 Display –> MQTT : subscribe to temperature<br />
 BMS –> MQTT : subscribe to temperature<br />
 Cloud –> MQTT : subscribe to temperature<br />
 ====</p>
<p> alt Periodic Publish</p>
<p> pub -> MQTT : Publish temperature</p>
<p> end</p>
<p> {start} Display -> Display : restart<br />
 Display -> MQTT : publish temperature.request</p>
<p> MQTT -> pub : publish temperature.request<br />
 activate pub<br />
 pub -> MQTT : Publish temperature<br />
 deactivate pub<br />
 {end} MQTT -> Display : Publish temperature<br />
 MQTT -> BMS : Publish temperature<br />
 MQTT -> Cloud : Publish temperature</p>
<p> {start} <-> {end} : minimal delay<br />

Pros: No need to create a response topic neither some additional handling that might be common to the event data

Cons: All components that subscribe to the data provided by the periodic event will also updated, this might not be desired is some cases.

Use case 2 – Option 2 – Add request/response pattern

The second option is to use a topic to request the data and another to publish the response, this is also known as a MQTT request/response design pattern.

There are two additional topics

Request topic: freezer/<guid>/temperature.request

Request payload: None

Response topic: freezer/<guid>/temperature.response

Response payload: Same payload as the topic freezer/<guid>/temperature

PlantUML Syntax: hide footbox<br />
 !pragma teoz true</p>
<p> participant “Temperature Publisher” as pub<br />
 participant “MQTT Broker” as MQTT<br />
 participant “Display” as Display<br />
 participant “Local BMS” as BMS<br />
 participant “Cloud Client” as Cloud</p>
<p> == startup ==<br />
 pub –> MQTT : subscribe to temperature.request<br />
 Display –> MQTT : subscribe to temperature<br />
 Display –> MQTT : subscribe to temperature.response<br />
 BMS –> MQTT : subscribe to temperature<br />
 Cloud –> MQTT : subscribe to temperature<br />
 ====</p>
<p> alt Periodic Publish</p>
<p> pub -> MQTT : Publish temperature</p>
<p> end</p>
<p> {start} Display -> Display : restart<br />
 Display -> MQTT : publish temperature.request</p>
<p> MQTT -> pub : publish temperature.request<br />
 activate pub<br />
 pub -> MQTT : Publish temperature.response<br />
 deactivate pub<br />
 {end} MQTT -> Display : Publish temperature.response</p>
<p>

Pros:

  • Selective usage of the request/reply pattern
  • simple way to use a pseudo data synchronization
  • Alternative way to retrieve data on demand

Cons:

  • Additional design and implementation complexity

Use case 3 – Ensure a response using the request/response pattern and the retain flag

One option is to implement a retry mechanism in the system. When a request is made to the component and it is not yet ready or available, the system can automatically retry the request at regular intervals until a response is received. This approach ensures that the component eventually receives the request, but it may lead to delays in receiving the information.

Another approach is to implement a notification system for the component. When the component is ready to receive requests, it can send a notification to the system indicating its availability. The system can then resend the request to the component. This approach reduces the chances of delays in receiving the information, as the component only receives the request once it is fully ready to process it.

Each of these design choices has its own set of advantages and disadvantages. The retry mechanism ensures that the component eventually receives the request, but it may result in delays. On the other hand, the notification system reduces the chances of delays but requires additional implementation for the notification mechanism. Ultimately, the choice between these two approaches will depend on the specific requirements and constraints of the system.

PlantUML Syntax:<br />
skinparam BackgroundColor transparent<br />
skinparam componentStyle rectangle</p>
<p>component “mqtt broker” as a<br />
component “Home Automation AI” as b<br />
component “Washing Machine” as c<br />
[b]<-right->[a] : publish requests<br />
[a]<->[c] : publish responses<br />

As previously mentioned, there are two choices available. The first option is to post the request using the retain flag, which ensures that the topic remains active during operation. Subsequent components that sign up for it will then be sent the published event along with the topic information.

PlantUML Syntax:<br />
hide footbox<br />
 !pragma teoz true</p>
<p> actor “Master” as M<br />
 participant “Home Automation AI” as AI<br />
 participant “MQTT Broker” as MQTT<br />
 participant “Washish Machine” as WM</p>
<p> alt Start Home Automation AI<br />
 AI -> MQTT : Subscribe \n to washish_machine/+/schedule.response<br />
 end</p>
<p> M -> AI : Please start \n the washing machine tomorrow at 8:00<br />
 AI –> MQTT : Publish \n washish_machine/<guid>/schedule.request \n retain=true</p>
<p> alt Washing Machine power on<br />
 Boot -> WM ** : Start<br />
 WM -> MQTT : Subscribe \n to washish_machine/+/schedule.resquest</p>
<p> end</p>
<p> MQTT –> WM : Publish\n  washish_machine/<guid>/schedule.request<br />
 WM –> MQTT : Publish \n washish_machine/<guid>/schedule.response</p>
<p> alt Clear retained topic<br />
  WM –> MQTT : Publish\n washish_machine/<guid>/schedule.request \n with empty payload<br />
 end</p>
<p>alt Washing Machine power on<br />
 Boot -> WM !! : re-start<br />
 WM -> MQTT : Subscribe\n to washish_machine/+/schedule.resquest<br />
 end<br />

Topic: washish_machine/+/schedule.resquest
Payload: {request_id:<unique id>, unix_time_stamp:<ts> , start_date:<int> }

Topic: washish_machine/+/schedule.response
Payload: {request_id:<unique id>, unix_time_stamp:<ts> , status:<OK|NOK>, status_message:<String> }

Another choice is to designate a subject for the publisher’s status update. This method also incorporates the Last Will and Testament (LWT) framework from MQTT. The LWT is established when the client connects and is broadcasted by the broker upon disconnection.

PlantUML Syntax:<br />
 hide footbox<br />
 !pragma teoz true</p>
<p> actor “Master” as M<br />
 participant “Home Automation AI” as AI<br />
 participant “MQTT Broker” as MQTT<br />
 participant “Washish Machine” as WM</p>
<p> alt Start Home Automation AI<br />
 AI -> MQTT : Subscribe to washish_machine/+/online<br />
 AI -> MQTT : Subscribe to washish_machine/+/offline<br />
 end</p>
<p> M -> AI : Please start the washing machine tomorrow at 8:00<br />
 AI -> AI : Store schedule (6:00)</p>
<p> alt Washing Machine power on<br />
 Boot -> WM ** : Start<br />
  WM -> MQTT : LWT to washish_machine/<guid>/offline<br />
  WM -> MQTT : Subscribe to washish_machine/+/schedule.resquest<br />
  WM -> MQTT : Publish washish_machine/+/online</p>
<p> end<br />
 MQTT -> AI ++ : Publish washish_machine/<guid>/online<br />
 AI -> AI : Check schedule<br />
 AI –> WM — : Publish washish_machine/<guid>/schedule.request</p>
<p> WM –> MQTT : Publish washish_machine/<guid>/schedule.response<br />
 MQTT –> AI ++ : Publish washish_machine/<guid>/schedule.response<br />
 AI -> AI –: Clear schedule</p>
<p>alt Washing Machine power off<br />
Boot -> WM !! : power off<br />
  MQTT -> AI : Publish washish_machine/<guid>/offline<br />
end</p>
<p>alt Washing Machine power on<br />
 Boot -> WM !! : re-start<br />
  WM -> MQTT : LWT to washish_machine/<guid>/offline<br />
  WM -> MQTT : Subscribe to washish_machine/+/schedule.resquest<br />
  WM -> MQTT : Publish washish_machine/+/online<br />
 end</p>
<p> MQTT -> AI ++ : Publish washish_machine/<guid>/online<br />
 AI -> AI : Check schedule (NONE)<br />

pros:

  • No need for the retain flag, this can be problematic for bridged brokers (!)
  • No need to clear retained topics

cons:

  • There is a need to manage the scheduled requests, this can be a topic if there are many schedules and there could be a need to store it persistently if the Home Automation shutdowns before the request reach the washing machine
  • More topics and respective subscriptions are required for the component status

Last Thoughts

In considering the design options for implementing a robust messaging system for edge and remote devices, it is crucial to account for the unique challenges presented by these devices. Given that they may not always be connected due to the nature of the technology or network constraints, it becomes imperative to create a system that can effectively handle intermittent connectivity and ensure reliable communication.

One possible design option is to incorporate a decentralized messaging architecture, where messages are distributed across multiple nodes in the network. This approach can help to ensure that messages are delivered even if some nodes are offline, reducing the risk of message loss or delays. Additionally, implementing message queuing systems can help to buffer messages during periods of network instability, ensuring that they are delivered once connectivity is restored.

Another design consideration is the use of lightweight communication protocols that are optimized for low-bandwidth and high-latency environments. By optimizing the communication protocols used for messaging, it is possible to reduce the overhead and latency associated with message transmission, improving the overall efficiency and reliability of the messaging system.

In addition to these design options, practical examples can be provided to illustrate how these principles can be applied in real-world scenarios. By drawing on experiences from the past seven years of working with global distributed IoT systems, valuable insights can be shared to inform the development of robust messaging systems for edge and remote devices. Through a combination of theoretical design principles and practical examples, a comprehensive understanding of effective messaging system design can be achieved.

Leave a Comment

Your email address will not be published. Required fields are marked *

IoT

MQTT – How to consume a snapshot value published in the past.

time to read: 16 min
Scroll to Top