Every way a device stays connected
Noticing the link is gone, getting back on without a stampede, keeping data meanwhile, and not doing anything twice. Each is a separate mechanism, and a robust device needs all four.
A connection will fail; the question is which part of surviving it you're designing. Work through them in order.
- Noticing it's gone. A TCP connection can die without either end being told. MQTT's keepalive bounds the delay to 1.5k seconds; a heartbeat or the last will tells everyone else. If someone must know within a minute, the keepalive is your setting.
- Getting back on. Reconnect with exponential backoff, a cap and jitter, so a short glitch costs a second and a long outage settles into rare attempts, and a thousand devices don't return in the same second. BLE links have their own version of this.
- Not losing data meanwhile. On the device, an offline queue (store and forward) with a stated drop policy. On the broker, a persistent session that queues QoS 1 and 2 messages for a client that comes back under the same ID. For the latest value only, a retained message.
- Not doing anything twice. Resending after an outage means some messages arrive twice. A message ID or correlation ID and an idempotent handler turn at-least-once delivery into exactly-once effects.
- Proving it works. Test it under a bad network on purpose, not by waiting for one.
Noticing the link is gone
Section
The network will fail
The failure modes, and the detection delay before anyone notices.
Term
keepalive
Example
Term
last will
Term
heartbeat
Getting back on
Section
Reconnect with backoff
Waits of base × factorⁿ, capped, with jitter.
Example
Term
backoff cap
Term
jitter
Section
BLE connection loss and recovery
The same problem on a BLE link, with its own supervision timeout.
Not losing data meanwhile
Section
Store and forward
An offline queue on the device, sized in RAM or flash, with a drop policy.
Term
drop policy
Section
Sessions and keepalive
A persistent session keeps subscriptions and queues QoS 1 and 2 messages for a client that returns under the same ID.
Not doing anything twice
Section
Retries and idempotency
Message IDs that survive a restart, and handlers that skip repeats.
Term
idempotent
Example
Term
message ID
Proving it works
Section
Testing under bad networks
Loss, delay and outages made on purpose, so recovery is tested before the field tests it.