All Products
Search
Document Center

ApsaraMQ for MQTT:Message expiry interval

Last Updated:Aug 23, 2024

This topic describes the message expiry interval feature of Message Queuing Telemetry Transport (MQTT) 5.0.

Feature description

You can use the message expiry interval feature of MQTT 5.0 to specify the maximum retention period of a published message in a broker. If the message is not delivered to a subscriber after the specified interval elapses, the message is deleted from the broker. The message expiry interval feature ensures the timeliness of messages and prevents the delivery of outdated information.

Limits

  • If you do not configure the message expiry interval feature, messages do not expire by default.

  • The value that you can specify for the Message Expiry Interval parameter ranges from 1 minute to 3 days.

  • If you specify a retention period that is shorter than 1 minute for the Message Expiry Interval parameter, 1 minute is used. If you specify a retention period that is longer than 3 days for the Message Expiry Interval parameter, 3 days is used.

Scenarios

  • Real-time monitoring: You can use the message expiry interval feature in Internet of Things (IoT) dashboards and real-time data monitoring systems. Outdated sensor data in the dashboards or systems may be inaccurate or irrelevant.

  • Real-time communication: You can use the message expiry interval feature in scenarios in which messages need to be sent to consumers in a specific period of time, such as time-limited discounts and flash sales.

  • Urgent messages: You can use the message expiry interval feature in scenarios in which messages must be immediately sent, such as alerts and security notifications.

  • Order and transaction processing: You can use the message expiry interval feature in scenarios in which a specific operation must be performed in a specific period of time, such as financial transactions and order processing.

Examples

Example 1: An IoT device is used to monitor and report the running status of the equipment in a factory. You can use MQTT messages in the following format and specify message expiry interval to process only the latest running status:

PUBLISH
Topic: factory/machine1/status
Payload: RUNNING
QoS: 1
Message Expiry Interval: 300 // The message is retained for 300 seconds (5 minutes) after being published.

If the message is not received by a subscriber within 5 minutes, the message is removed from the queue on the broker to prevent other subscribers from receiving outdated information.

Example 2: An e-commerce company wants to send a message about a time-limited discount. Sample code:

PUBLISH
Topic: store/promotions
Payload: 50off-for-next-30-minutes
QoS: 1
Message Expiry Interval: 1800 // The discount expires in 1800 seconds (30 minutes).

If the message is not received by a subscriber within 30 minutes, the message is expired and automatically deleted from the broker.

The major benefit of the message expiry interval feature is to ensure that only the latest and relevant information is delivered to subscribers.