MQTT persistent session vs. Clean Session insider information

1 Introduction

  • unstable network
  • Limited hardware resources

Two major problems in IoT applications are that the connection between the MQTT client and the server may be abnormally disconnected at any time due to network fluctuations and resource limitations. In order to solve the impact of network connection disconnection on communication, the MQTT protocol provides a persistent session function.

The MQTT client can set whether to create a persistent session when initiating a connection to the server. A persistent session saves important data so that the session can continue across multiple network connections.

2 functions

  • Avoid the additional overhead of repeated subscriptions due to network outages
  • Avoid missing messages while offline
  • Ensure QoS 1 and QoS 2 message quality guarantees are not affected by network outages

3. What data needs to be stored in a persistent session?

From the above we know that a persistent session needs to store some important data so that the session can be restored. Some of these data are stored on the client side, and some are stored on the server side.

Session data stored in client:

  • QoS 1 and QoS 2 messages that have been sent to the server but have not been confirmed yet
  • QoS 2 message received from the server but not confirmed yet

Session data stored on the server:

  • Whether the session exists, even if the rest of the session state is empty.
  • QoS 1 and QoS 2 messages that have been sent to the client but have not yet been acknowledged.
  • Waiting for QoS 0 messages (optional), QoS 1 and QoS 2 messages to be transmitted to the client.
  • QoS 2 messages received from the client but not yet acknowledged, testament messages and testament delay intervals.

4 MQTT Clean Session

Flags that control session state life cycle:

  • trueIndicates creating a new session, which will be automatically destroyed when the client disconnects.
  • falseIndicates the creation of a persistent session, which remains maintained after the client disconnects until the session times out and logs out.

The premise that a persistent session can be restored is that the client uses a fixed Client ID to connect again. If the Client ID is dynamic, a new persistent session will be created after the connection is successful.

As shown in the following Dashboard, although the connection in the picture is disconnected, it can still be viewed because it is a persistent session, and the session can be cleared manually in the Dashboard:

MQTT persistent session

EMQX also supports setting Session related parameters in the Dashboard.

EMQX Dashboard

MQTT 3.1.1 does not specify when a persistent session will expire. It only understands from the protocol layer that this persistent session should exist permanently. But the actual scenario is unrealistic because it takes up a lot of server resources, so the server actually provides users with a global configuration to limit the session expiration time. For example, if the session expiration time is set to 5 minutes, the maximum number of messages is 1000, and QoS 0 messages are not saved.

5 use

New ConnectionAfter opening MQTTX, click the button to create an [MQTT connection] as shown below .

MQTT Client

Create MQTT_V3a connection named , Clean Session is closed (that is, false), select MQTT version 3.1.1, and then click Connectthe button in the upper right corner.

Create MQTT connection

After the connection is successful, clean_session_falsethe topic is subscribed and QoS is set to 1.

Subscribe to an MQTT topic

After the subscription is successful, click the disconnect button in the upper right corner. Then, create a MQTT_V3_Publishconnection named and set the MQTT version to 3.1.1. After the connection is successful, clean_session_falsetwo QoS 1 messages will be published to the topic.

Publish MQTT messages

Then select the MQTT_V3 connection, click the connect button to connect to the server, and you will successfully receive two offline messages.

Receive MQTT messages

6 Session improvements in MQTT 5.0

In MQTT 5.0, Clean Session is split into:

  • Clean Start

    Specifies whether to create a completely new session when connecting or attempt to reuse an existing session

  • Session Expiry Interval

    Specifies the session expiration time after the network connection is disconnected

A Clean Start of truemeans that any existing session must be discarded and a completely new session must be created; a Clean Start falseof means that the session associated with the Client ID must be used to resume communication with the client (unless the session does not exist).

Session Expiry Interval solves the problem of wasting server resources caused by the persistence of persistent sessions in MQTT 3.1.1. Set to 0 or not set, indicating that the session will expire when the connection is disconnected; set to a value greater than 0, indicating how many seconds the session will remain after the network connection is closed; set to 0xFFFFFFFFindicate that the session will never expire.

7 FAQ

When the session ends, do retained messages still exist?

[MQTT retained messages] are not part of the session state and they are not deleted at the end of the session.

How does the client know that the current session is the resumed session?

Starting from v3.1.1, the MQTT protocol has designed the Session Present field for CONNACK messages. When the value of this field returned by the server is 1, it means that the current connection will reuse the session saved by the server. The client can use this field value to decide whether it needs to resubscribe after the connection is successful.

Recommendations for using persistent sessions

  • Dynamic Client ID cannot be used, and the Client ID needs to be fixed every time the client connects.
  • Reasonably evaluate the session expiration time based on server performance, network conditions, client type, etc. Setting it too long will occupy more server resources, and setting it too short will cause the session to expire without successful reconnection.
  • When the client determines that the session is no longer needed, it can use Clean Session to be true to reconnect, and then disconnect after the reconnection is successful. If it is MQTT 5.0, you can directly set the Session Expiry Interval to 0 when disconnecting, which means that the session will become invalid after the connection is disconnected.

8 Summary

This completes the introduction to MQTT persistent sessions, and demonstrates the use of Clean Session through the desktop client. Readers can refer to this article to use MQTT persistent sessions to receive offline messages and reduce subscription overhead.

About the author: A technical expert from a state-owned enterprise in Shanghai, with experience in backend R&D and architecture for many large manufacturers. He is responsible for the modular, service-oriented, and platform-based R&D of extremely complex business systems. He has rich experience in leading teams and has profound accumulation of talent identification and training.

reference:

Programming Selection Network

This article is published by OpenWrite, a blog that publishes multiple articles !

Broadcom announced the termination of the existing VMware partner program . Site B crashed twice, Tencent's "3.29" level one incident... Taking stock of the top ten downtime incidents in 2023, Vue 3.4 "Slam Dunk" released, Yakult confirmed 95G data Leaked MySQL 5.7, Moqu, Li Tiaotiao... Taking stock of the (open source) projects and websites that will be "stopped" in 2023 "2023 China Open Source Developer Report" is officially released Looking back at the IDE 30 years ago: only TUI, bright background color …… Julia 1.10 officially released Rust 1.75.0 released NVIDIA launched GeForce RTX 4090 D specially for sale in China
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3494859/blog/10570103