MQTT 5 replaced the single Clean Session choice with two controls that answer different questions. Clean Start tells the broker what to do with Session State at the moment a new connection is accepted. Session Expiry Interval tells both sides how long to retain the resulting Session after the network connection closes.
Robot clients need both decisions. Treating Clean Start as a general persistence switch can either erase useful subscriptions during a short outage or leave stale queued work at the broker much longer than intended.
Clean Start decides the opening boundary
Clean Start is one bit in CONNECT. When it is 1, the client and server discard any existing Session associated with that Client Identifier and start a new Session. Because the old state is deliberately discarded, CONNACK reports Session Present 0.
When Clean Start is 0, the server looks for an existing Session for the Client Identifier. If state exists, it resumes communication from that Session. If none exists, it creates a new Session. Clean Start 0 therefore requests continuity; it does not guarantee continuity.
The client must read Session Present in CONNACK. A value of 1 confirms that the server found and resumed state for this Client Identifier. A value of 0 means the client needs to rebuild subscriptions and any related local mapping, even though it asked for a continuation.
Session Expiry decides the closing boundary
Session Expiry Interval is a Four Byte Integer in CONNECT, measured in seconds. If it is absent, its value is zero. A zero interval means the Session ends when the network connection closes. A positive value requires the client and server to retain Session State after disconnection for that interval. The maximum value, 0xFFFFFFFF, means the Session does not expire on a timer.
This retention timer is independent of whether the connection began with a clean slate. A client can start a brand-new Session and still ask the broker to keep it after disconnect. It can also resume an old Session and request zero expiry, causing that Session to end when the current connection closes.
The client can change the interval in DISCONNECT under the MQTT 5 rules. This supports an orderly shutdown that releases broker state after a period of intermittent reconnects. Long retention should be intentional because stored subscriptions and queued QoS messages consume resources.
Four combinations produce distinct behavior
Clean Start 1 with Session Expiry 0 is an ephemeral connection. Existing Session State is discarded at connect, and the new state ends at disconnect. A diagnostic tool or temporary robot commissioning client often fits this pattern.
Clean Start 1 with a positive expiry creates a new persistent Session. Old state is discarded, but subscriptions made during this connection can survive a later outage. This is useful after a deliberate configuration reset.
Clean Start 0 with a positive expiry requests resume and continued retention. It is the normal pattern for a robot gateway that must survive brief network loss. Session Present determines whether subscriptions and queued delivery actually resumed.
Clean Start 0 with expiry 0 requests resume for the current connection but asks that state end at disconnection. It can consume existing queued information now without leaving the Session behind afterward.
Session State is specific
For the server, Session State includes subscriptions and queued QoS 1 or QoS 2 messages addressed to the client, plus the protocol state needed for reliable delivery. For the client, it includes outbound and inbound QoS state. Application databases, robot controller memory, and a process command's completion status are outside MQTT Session State.
Topic Alias mappings are also not retained across network connections. A resumed Session can restore subscriptions while still requiring every alias to be established again. Similarly, the client's local map from Subscription Identifiers to callbacks must remain aligned with the resumed server state.
Design a reconnect policy
Choose a stable Client Identifier for each logical robot gateway. On routine reconnect, use Clean Start 0 and a finite positive expiry long enough to cover expected outages. Read Session Present before deciding whether to resubscribe.
If Session Present is 1, keep local subscription and QoS state consistent with the resumed Session. Blindly subscribing again can modify options or identifiers and may complicate retransmissions. If Session Present is 0, clear local assumptions, establish subscriptions afresh, and reset callback mappings.
Use Clean Start 1 only for a deliberate state reset, client identity change, or recovery from known inconsistency. Record the reason because this action discards broker-held Session State for the Client Identifier.
Select expiry from operational evidence. A five-minute window might cover wireless roaming; a several-hour window may support maintenance. Infinite retention requires explicit lifecycle cleanup. When the gateway is retired, send a normal DISCONNECT with Session Expiry Interval 0 or use an equivalent administrative cleanup.
Test timing and state separately
Create a subscription with Clean Start 1 and positive expiry, disconnect without exceeding the interval, then reconnect with Clean Start 0. Verify Session Present 1 and confirm queued QoS traffic resumes. Wait beyond expiry and repeat; Session Present should be 0 and the client should resubscribe.
Next reconnect with Clean Start 1 before expiry and confirm that existing state is discarded immediately. Test a connection that resumes state but sets expiry to zero, then verify state is gone after disconnection. Include broker restart behavior and configured persistence limits in deployment testing.
Clean Start controls whether the next connection begins with existing Session State. Session Expiry controls how long the resulting state survives after that connection closes. Modeling both boundaries lets a robot client recover reliably without confusing a new connection, a resumed Session, and durable application state.