MQTTv5 Clean Start (Clean Sessions) and Session Expiry

In MQTT v3.1.1 we have the concept of clean sessions or non persistent/persistent connections.

On connect the client indicates to the server using the clean session flag if the session state should be kept by the server when the client disconnects.

Session state includes any client subscriptions and also messages depending on the QOS used and is covered in detail in the Understanding clean sessions tutorial , I recommend you read before proceeding if you are unfamiliar with clean sessions.

MQTTv5 Clean Start

In MQTTv5 clean sessions is now known as clean start, and is used in conjunction with a new feature called the session expiry interval.

Setting clean start to True is the same as setting clean sessions to True and Setting clean sessions to False is the same as setting clean sessions to False.

However when clean sessions is False you can also set a session expiry interval. After this interval the server will expire the client and clear the session state.

This overcomes the problem in 3.1.1 were the session state would be stored indefinitely if the client didn’t reconnect and clear it.

The clean session interval is a 4 byte integer and setting it to 0xFFFFFFFF means the session doesn’t expire.

If the session expiry interval is not defined a value of 0 is used which means that the session expires when the client disconnects.


Set clean start to False and a session expiry interval to store the session state. This is equivalent to clean session False but with a timeout.

Set clean start to True and the session expiry interval to 0 to not store the session state. This is equivalent to clean session True.

Python Code Demo

The following screen shot show the code running. It starts by setting the session expiry interval to 30 seconds and disconnecting. Messages are then published and should be queued on the broker.

If the client reconnects within 30 seconds it receives the messages if it connects after 30 seconds it doesn’t receive the messages as the broker has timed out the session.



Demo Script


Important – Clean Sessions and Client Id

With persistent connections the connection details are stored against the client id and so when using persistent connections (clean session False) you cannot use a random client Id.

Related Tutorials and Resources

Please rate? And use Comments to let me know more


  1. Hi, I’m curious in paho.mqtt.c there is an option of PERSISTENCE storage, where
    if client1 sends publish/subscribe message then if persistence is enabled it stores the message in local file,
    Whats the difference with clean start here.

    1. Not familiar with the C client but I have a feeling that this is related to the broker side i.e Mosquitto.

  2. Two important notes about Clean Start = false
    1. Client libraries sometimes (often?) defaults to protocol version 4 (3.1.1) which means that the server will ignore the expiry interval. Be sure to set protocol version 5. At least when using when using random client id:s.
    2. Always consider to clean start = true. If false, be sure to use expiration interval as short as possible. When using false combined with random client id:s and none or long interval, the server will suffocate.

    Mosquitto is equipped with a nonstandard server variable that let you set an upper limit of expiry interval. Please use it!

Leave a Reply

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