logo elektroda
logo elektroda
X
logo elektroda

MQTT broker restart: retaining values with Mosquitto, EMQX, HiveMQ, persistent sessions, QoS

User question

How to make MQTT to send values after the MQTT broker has restarted

Artificial Intelligence Response

Direct answer to the question

• Publish the data with the retain flag set and make sure the broker is configured for persistence.
• If you also need to guarantee delivery of data generated while the broker was down, let the publishers connect with a static client-ID, Clean-Session = false (Clean-Start = false in MQTT 5) and QoS 1 or 2, and enable persistence on the broker.
Key points
– Retained messages guarantee that the last value on a topic survives a broker restart and is sent immediately to any client that (re-)subscribes.
– Persistent sessions + QoS > 0 guarantee that every queued message produced while the broker was unreachable is delivered after the broker and the client reconnect.

Detailed problem analysis

  1. Broker restart effects
    • All TCP sessions are dropped → clients disconnect.
    • Volatile state (subscriptions, in-flight packets, retained messages, queued messages) is lost unless the broker has been told to persist them.

  2. Two complementary MQTT mechanisms
    a) Retained messages
    – One message per topic, stored by the broker.
    – Delivered to any subscriber immediately after it subscribes, irrespective of when it connected.
    – Survives restarts only if broker persistence is enabled.
    b) Persistent sessions (session queuing)
    – Achieved by ClientID = "fixedId", CleanSession = false (MQTT ≤3.1.1) or CleanStart = false, Session Expiry Interval > 0 (MQTT 5).
    – Works only for messages published with QoS 1 or QoS 2.
    – The broker stores:
    • subscription list
    • undelivered outgoing messages
    • QoS handshake state
    – When the client reconnects it receives the backlog automatically.

  3. Practical publisher flow during an outage
    • Client detects disconnect (TCP error / on_disconnect).
    • Continues to buffer outgoing QoS 1/2 messages (library does this automatically; add a disk-backed queue if you can lose power).
    • Periodically retries connect().
    • On successful reconnect with the same ClientID and CleanSession = false, the session resumes and all buffered messages flush to the broker.

  4. Broker-side requirements (examples)
    Mosquitto 2.x (mosquitto.conf)

    persistence true
    persistence_location /var/lib/mosquitto/
    autosave_interval 30

    EMQX 5.x (emqx_retainer.conf)

    retainer.storage_type = both

    HiveMQ CE (config.xml)

    <persistence>
    <payload-store enable="true"/>
    </persistence>

Current information and trends

• MQTT 5 adds granular control: Session Expiry, Message Expiry, Will Delay, Shared Subscriptions.
• Brokers increasingly support external persistence engines (Kafka, Redis, Timeseries DB) to survive crashes and allow analytics.
• Edge devices start using store-and-forward patterns (e.g., Eclipse Hono, AWS Greengrass) that marry MQTT session persistence with local SQLite/Flash queues for >99.999 % availability.

Supporting explanations and details

• QoS levels
– 0 = “fire-and-forget”; never queued.
– 1 = at-least-once; possible duplicates.
– 2 = exactly-once; extra 2-way handshake, more overhead.
• Retained ≠ Queued: a retained message is overwritten by the next retained publish, while queued messages form a FIFO backlog per client.
• Clearing a retained message: publish the same topic with an empty payload and retain=true.
• Code snippet (Paho-Python publisher)

 import paho.mqtt.client as mqtt, time, json
cid = "sensor-t800"
client = mqtt.Client(client_id=cid, clean_session=False)
client.connect("broker.local", 1883, 60)
client.loop_start()
while True:
value = read_temperature()
payload = json.dumps({"t": value, "ts": time.time()})
client.publish("plant/temperature", payload, qos=1, retain=True)
time.sleep(10)

– The same code continues to publish; if the broker restarts, Paho’s internal queue keeps QoS 1 messages until the TCP link is back.

Ethical and legal aspects

• Security: enable TLS and client authentication so that automatic reconnects do not open a replay or spoofing vector.
• Privacy: retained messages may expose sensitive state to any subscriber; apply ACLs.
• Safety: for command topics use QoS 2 plus application-level idempotency to avoid duplicate actuation.

Practical guidelines

Implementation checklist

  1. Broker
    persistence true and path on disk with enough space.
    • Configure autosave (Mosquitto) or durable storage backend (EMQX “both”).
  2. Publisher / Subscriber
    • Static ClientID.
    clean_session=False / clean_start=False.
    • QoS 1 or 2 for critical data.
    retain=True for current value topics.
    reconnect_delay_set() (Paho) or similar back-off.
    • Optional: local persistent queue (SQLite/Flash) for brown-out resilience.
  3. Test procedure
    • Publish retained test message.
    • Stop broker, keep publishing every second.
    • Start broker, verify:
    – retained message delivered to new client immediately;
    – backlog flushes to broker;
    – no duplicates beyond QoS guarantees.

Potential challenges & mitigation
• Large backlog → tune max_queued_messages and storage location.
• Flash wear on microcontrollers → batch-write queue.
• Broker disk full → enable log rotation & alerts.

Possible disclaimers or additional notes

• Retained messages alone are not suitable for full telemetry history. Use a time-series DB or MQTT-to-Kafka bridge.
• Some lightweight brokers (earlier Mosquitto 1.x builds, uMQTT on MCUs) do not persist retained messages; upgrade or switch.
• Very old MQTT 3.1 devices might ignore Session Expiry semantics of MQTT 5.

Suggestions for further research

• Evaluate MQTT SN or CoAP Observe for sensor networks with aggressive sleep cycles.
• Study MQTT Sparkplug B for industrial state replication (birth certificates + death certificates).
• Look at Persistent Data Exchange Patterns in Eclipse Tahu and W3C Web of Things.
• Explore edge-cloud synchronization frameworks (Azure IoT Edge, AWS IoT Greengrass v2).

Brief summary

To keep values available after the broker restarts:

  1. Publish each topic’s latest value with retain = true and enable broker-side persistence so that value is stored on disk.
  2. For continuous streams created while the broker is down, configure clients with a fixed ClientID, persistent session (CleanSession = false / CleanStart = false) and QoS 1 or 2; ensure both client and broker persist the session.
  3. Add automatic reconnect logic and, if ultimate reliability is required, a disk-backed queue on the client.
    Following these steps makes MQTT infrastructures resilient to broker restarts without data loss or state blindness.

Disclaimer: The responses provided by artificial intelligence (language model) may be inaccurate and misleading. Elektroda is not responsible for the accuracy, reliability, or completeness of the presented information. All responses should be verified by the user.