Skip to content

LwM2M Client Retransmission Causes Duplicate Messages

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about LwM2M Client Retransmission Causes Duplicate Messages. We cover key concepts, practical examples, and best practices.

The Problem

LwM2M client retransmission after timeout causes duplicate messages on the server.

Quick Fix

Wrong

// No deduplication — client retransmits entire request
anjay_coap_send(request);  // Timeout
anjay_coap_send(request);  // Retransmit as new```

Server receives and processes the same request twice. Duplicate actions.


### Right

```cpp
#include <anjay/anjay.h>
#include <anjay/core.h>

void custom_retransmit_handler(anjay_t *anjay,
    const anjay_coap_request_t *req) {
  // Check Message ID for deduplication
  uint16_t msg_id = anjay_coap_get_msg_id(req);

  // Track retransmitted message IDs
  static uint16_t last_msg_id = 0;
  static uint32_t last_send_time = 0;

  if (msg_id == last_msg_id &&
      millis() - last_send_time < 5000) {
    // Duplicate detected — ignore
    Serial.println("Duplicate detected, ignoring");
    return;
  }

  last_msg_id = msg_id;
  last_send_time = millis();

  // Anjay handles CoAP retransmission automatically:
  // - CON messages: retransmit with exponential backoff
  // - Max 4 retransmissions
  // - Initial timeout: 2s, doubles each time
  // - Total timeout: ~45s before giving up
  anjay_coap_send(req);
}

int main(void) {
  anjay_t *anjay = anjay_new(&config);
  anjay_coap_set_retransmission_handler(anjay,
    custom_retransmit_handler);

  anjay_start(anjay);
}```

First request: sent with Message ID=42. Timeout, retransmitted with same ID. Server deduplicates by Message ID. No duplicate actions.


## Prevention

CoAP retransmission uses Message IDs for deduplication. The server detects duplicates by (Source IP, Message ID) tuple within an EXCHANGE_LIFETIME window (typically 247 seconds). Retransmission follows exponential backoff: initial 2s, *1.5 each retry, max 4 retransmissions. Anjay handles this automatically for CON messages.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### How does CoAP detect duplicates?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>By (Source IP, Port, Message ID) within the EXCHANGE_LIFETIME (247s). The same Message ID with different Token is a new response to the same request.</p>
<h3 id="what-is-the-retransmission-schedule">What is the retransmission schedule?</h3><p>Start 2s, wait, retry at 3s (+1.5x), 4.5s, 6.75s. Total ~45s before giving up. Configurable in most CoAP stacks.</p>
<h3 id="can-i-disable-retransmission">Can I disable retransmission?</h3><p>For NON messages, there is no retransmission. For CON, retransmission is built into the protocol. Use NON for fire-and-forget messages.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro