After a power cut, three lanes of trucks queue bumper to bumper at one gate, and the connection gauge is full.

An outage in a building looks harmless from the device side. Wi-Fi drops for ten minutes, or the power, or the server restarts. The trouble starts when the outage ends, because every device that was waiting tries to reconnect in the same second. This article is part of the series “Production details”, after why IoT devices can take a website down and time before TLS.

What happens when every device reconnects at once?

When every device reconnects at once, the server or the database behind it gets a burst of new TLS handshakes and sessions within seconds. Each handshake costs CPU, each session a slot. If the burst is larger than what the endpoint accepts, connections fail, and the devices retry. With a fixed retry interval, all of them retry after exactly the same delay, so the next burst is as large as the first. The whole fleet then keeps hitting the same limit together.

The limit can be small. A database session pool of 15, shared with a website, is enough to take both down, as described in why IoT devices can take a website down.

Why does retry logic belong in the first firmware?

Retry logic belongs in the first firmware because it runs on the device and an outage is the moment when no update can reach it. A fleet that ships with a fixed retry interval will storm the server after every outage until every device has new firmware, and the update can only be delivered once the storm settles. Microsoft’s IoT Hub guidance prescribes exponential backoff with jitter for every device reconnect (Microsoft, managing device reconnections), and AWS ships a backoff library with jitter for FreeRTOS devices (AWS, backoffAlgorithm library).

How does exponential backoff with jitter work?

Exponential backoff with jitter makes each device wait a random time below a ceiling that doubles after every failure. After the first failure the ceiling is, for example, 2 seconds, then 4, 8, 16, up to a cap. Each device picks a random wait between zero and the ceiling, so devices that failed together retry at different moments. AWS calls this variant “full jitter” and showed that it reduces both the total number of calls and the time until all clients are through (AWS Architecture Blog).

Backoff with jitter: after the same outage, trucks arrive spread over a minute, each retry waits about twice as long plus a random part, and the gate shows connects 18 percent.
Same outage, spread over a minute: the endpoint sees a few connections per second instead of the whole fleet at once.

How do I implement it on an ESP32 with ESP-MQTT?

Turn off ESP-MQTT’s fixed automatic reconnect and schedule the reconnect yourself after a randomised delay:

#include "mqtt_client.h"#include "esp_random.h"#include "esp_timer.h"#define BACKOFF_BASE_MS   2000#define BACKOFF_CAP_MS  300000   /* 5 minutes */static esp_mqtt_client_handle_t client;static esp_timer_handle_t retry_timer;static uint32_t attempt;static void retry_cb(void *arg) { esp_mqtt_client_reconnect(client); }static void schedule_reconnect(void){    uint64_t ceiling = (uint64_t)BACKOFF_BASE_MS << (attempt < 8 ? attempt : 8);    if (ceiling > BACKOFF_CAP_MS) ceiling = BACKOFF_CAP_MS;    uint64_t wait_ms = esp_random() % (ceiling + 1);      /* full jitter */    attempt++;    esp_timer_start_once(retry_timer, wait_ms * 1000);}static void mqtt_event(void *arg, esp_event_base_t base, int32_t id, void *data){    if (id == MQTT_EVENT_CONNECTED)    attempt = 0;    if (id == MQTT_EVENT_DISCONNECTED) schedule_reconnect();}void start_mqtt(const esp_mqtt_client_config_t *base_cfg){    esp_mqtt_client_config_t cfg = *base_cfg;    cfg.network.disable_auto_reconnect = true;   /* no fixed retry interval */    client = esp_mqtt_client_init(&cfg);    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event, NULL);    const esp_timer_create_args_t t = { .callback = retry_cb, .name = "mqtt_retry" };    esp_timer_create(&t, &retry_timer);    /* Spread the first connection after boot too: devices in one building boot together. */    vTaskDelay(pdMS_TO_TICKS(esp_random() % 30000));    esp_mqtt_client_start(client);}

Three more things help:

  1. Jitter the regular reports. A sensor that reports “every 15 minutes” should report every 15 minutes plus or minus a random offset, so the fleet does not drift into the same second.
  2. Reset only after success. Set the attempt counter back to zero when the connection is up, not when the attempt starts.
  3. Battery devices go back to sleep. A battery device that fails should not keep its radio on for a long backoff. It sleeps and tries again on the next wake, with its own random offset.

What else spreads the load?

Two things on the server side complement the device. A rate limit per project or per device at the endpoint turns a storm into a queue instead of an outage. A buffer between the endpoint and the database keeps a burst from reaching the database all at once. Neither replaces backoff on the device, because a rate limit that rejects connections causes more retries if the devices retry in step.

How do I test a reconnect storm?

Test it by simulating the outage, not the steady state. Run a few hundred simulated clients with the same firmware logic against a test endpoint, cut the endpoint for ten minutes and bring it back. Record connection attempts per second, failures and the time until every client is connected. With a fixed interval the attempts come in waves of the full fleet. With full jitter they spread into a curve that stays under the endpoint’s limit.

Sources

Frequently asked questions

What is a reconnect storm?

A reconnect storm is the burst of connection attempts that follows an outage, when every device that lost its connection tries to reconnect at the same moment. If the attempts fail, devices with the same retry timer try again together, so the burst repeats.

What is exponential backoff with jitter?

A retry strategy where the maximum wait doubles after every failed attempt, up to a cap, and each device waits a random time between zero and that maximum. The doubling reduces the total load, and the randomness keeps devices from retrying in step.

Does ESP-MQTT have backoff with jitter built in?

ESP-MQTT reconnects automatically after a fixed reconnect_timeout_ms. For backoff with jitter, disable the automatic reconnect and schedule esp_mqtt_client_reconnect() yourself after a randomised delay.

What cap should the backoff have?

A cap that matches how long the product can afford to be offline, often a few minutes for mains-powered devices and the next regular wake for battery devices. Without a cap, a device can wait for hours after a long outage.

Why also add jitter to the first report?

Because after a power cut all devices in a building boot within seconds of each other. If they all report immediately, the first report is itself a burst. A random offset of a few seconds to a minute spreads it.