MQTT离线队列消息数量限制及LIFO存储方式问询
Hey there! Let's break down your two questions about MQTT offline message queues step by step:
The approach depends on which MQTT broker you're using, since each has its own configuration options. Here are common setups for popular brokers:
Mosquitto
- In your
mosquitto.conffile, use themax_queued_messagesdirective to set the maximum number of offline messages per client (default is 100). For example:max_queued_messages 500 - If you want to exclude QoS 0 messages from the offline queue (since QoS 0 doesn't guarantee delivery), set
queue_qos0_messages false—this reduces queue size significantly for clients sending lots of QoS 0 data. - Remember: This only applies when the client connects with
clean_session=false(sinceclean_session=truetells the broker not to store offline messages at all).
- In your
EMQX
- You can set a global limit for all clients via the broker's configuration (e.g., in
emqx.conf):mqtt.max_offline_messages = 1000 - For per-client limits, use client ACL rules or the EMQX Dashboard to override the global setting for specific clients.
- Additionally, you can configure
mqtt.queue_qos0_messagesto control whether QoS 0 messages are queued for offline clients.
- You can set a global limit for all clients via the broker's configuration (e.g., in
HiveMQ
- Use the
max-messages-per-sessionsetting in the broker configuration to cap the offline queue size per client. You can also set this per client via client profiles in the HiveMQ Control Center.
- Use the
A general best practice: If you're dealing with resource constraints, combine queue limits with setting appropriate QoS levels—only use QoS 1/2 for messages that truly need reliable offline delivery, and let QoS 0 messages drop when the client is offline if they're not critical.
By default, all MQTT brokers use FIFO (First-In-First-Out) for offline message queues, which aligns with the protocol's intent of delivering messages in the order they were published.
That said, some modern brokers support LIFO (Last-In-First-Out) as an optional configuration, since the MQTT spec doesn't mandate a specific storage order. Here's how to enable it in common brokers:
- EMQX: Set the
mqtt.queue_modeconfiguration tolifo(default isfifo). This makes the broker deliver the most recent messages first when the client reconnects, which is useful for use cases like sensor data where only the latest reading matters. - HiveMQ: Use custom extensions or the HiveMQ Enterprise Edition's message prioritization features to implement LIFO behavior for specific client queues.
- Note: Not all brokers support LIFO—always check your broker's documentation to confirm if this feature is available. If your broker doesn't support it natively, you might need to handle message prioritization on the publisher side (e.g., only keeping the latest message for a specific topic and overwriting older ones).
内容的提问来源于stack exchange,提问作者whiteFang

