Can I safely set the message expiry to very high

Hello all,

In our scenario we want to get the last values of all the topics when you first connect (or with a clean session). This is of course a perfect candidate to just have all the messages with retained(true).

However, when reading the message expiry setting. I see: A retained message that is stored on the broker expires.

Am I right in assuming that this setting will remove retained messages that are older than the message expiry setting? If so, that is something that we do not want, so we could solve it by setting the message expiry to a very large number. However, does that mean that it will keep all messages. So not just the messages that are retained? That would imply a massive amount of messages that would get saved right?

Am I understanding it right?

Thanks!

Hello @basimons

Yes, your understanding is mostly correct. The implications of setting a very large expiry interval would effectively prevent retained messages from expiring, but this setting applies individually to each message that has the interval set. It doesn’t mean the broker will keep all messages indefinitely only those with the expiry interval set.

Kind regards,
Diego from HiveMQ Team

Hi @basimons

Thank you for your questions regarding retained messages and the associated message expiry setting.

When exploring the message expiry setting outlined in the documentation at MQTT Specific Configuration · hivemq/hivemq-community-edition Wiki · GitHub, it’s crucial to understand that this pertains to the broker-side configuration for the maximum message expiry interval. You can find detailed information about this setting here: HiveMQ Configuration :: HiveMQ Documentation.

For MQTT version 3 clients, the specification of the publish message expiry interval individually is not an option. In such cases, the broker-side setting is applied, as detailed here: HiveMQ Configuration :: HiveMQ Documentation.

For MQTT version 5 clients, there is the flexibility to specify the message expiry interval individually for each PUBLISH message. This feature allows you to set a longer interval for retained messages and a shorter interval for other messages as needed.

However, in cases where accommodating MQTT v.3 clients becomes imperative, a custom HiveMQ Extension can be created, using HiveMQ Extension SDK. By doing so, it becomes possible to intercept all incoming MQTT v.3 publish packets and add a message expiry interval setting to them: Interceptors :: HiveMQ Documentation

Concerning the potential accumulation of messages with a longer message expiry interval for retained messages, rest assured that only the last retained message per topic is stored.

I hope it helps. Should you have further questions please don’t hesitate to reach out.

Best regards,
Dasha from HiveMQ Team

Awesome, thanks for both of your extensive explanations, it sounds like I can just put it at the maximum.