Trending

World technology news

→
CoAP after a lost ACK: recognizing a repeated sensor read
Software Engineering · 2014
Software Engineering2014

CoAP after a lost ACK: recognizing a repeated sensor read

A local model traces a lost acknowledgement and shows how CoAP Message IDs and Tokens serve different roles.

Archive retrospective · Topic year 2014

Developer's JournalPublished: Dec 6, 20266 min read
Reading progress0%
In this article
  1. Preserve the sampling timestamp
  2. A sensor answers, but the reply disappears
  3. A Message ID and a Token serve different layers
  4. Test the loss as a small in-memory fixture
  5. The duplicate record needs a bounded lifetime
  6. An acknowledgement is distinct from the underlying work
  7. Give each observable event its own counter

Preserve the sampling timestamp

If the reading takes measurable time to obtain, gateway reception time cannot identify the start of the physical sample. Keep a sample number and a timestamp with the response so a recovered packet does not create a second point in a chart. Record reception separately and account for an unreliable device clock. This makes it possible to distinguish network delays from a fault in the sampling process.

A sensor answers, but the reply disappears

A warehouse gateway asks a low-power temperature node for a reading. The node receives the request, samples 18.5 degrees, and replies. With no packet back, the gateway cannot tell whether its request never arrived or the response was lost on the return journey. Retrying is expected. The receiver needs to determine whether the arriving datagram requests another measurement or repeats the same message. RFC 7252, published in June 2014, describes how CoAP distinguishes these cases. Delivery control, the resource the client asks for, and the resulting reading belong to different parts of the exchange. A sender can retransmit the same confirmable message after a timeout. A receiver that has already processed it can recognize that message and avoid repeating the underlying operation.

Application flowConceptual sketch · summary of the article's technical flow
Illustrative diagram. The receiver recognizes the same peer and Message ID.

A Message ID and a Token serve different layers

A confirmable message expects an acknowledgement. At the message layer, the acknowledgement uses the same Message ID as the message being acknowledged. The request in the fixture uses 4200. A Token instead correlates an application response with the request that prompted it. When the resource handler can answer quickly, the acknowledgement can carry the response. When computation takes longer, an empty acknowledgement can arrive first and a separate response can follow. The client cannot infer from an empty ACK that the reading has been produced. It tracks the later response using the request Token. Keeping both values in a trace helps an operator distinguish a successfully delivered request from a final measurement that has yet to arrive.

import assert from 'node:assert/strict';

const seen = new Map();
let samples = 0;
function receiveRead({ peer, messageId, token }) {
  const key = JSON.stringify([peer, messageId]);
  if (seen.has(key)) return seen.get(key);
  samples++;
  const ack = { type: 'ACK', messageId, token, code: '2.05', payload: 18.5 };
  seen.set(key, ack);
  return ack;
}

const request = { peer: 'gateway-a', messageId: 4200, token: 'a7' };
const first = receiveRead(request);
const second = receiveRead(request); // Sender retries because the first ACK is lost.
assert.deepEqual(first, second);
assert.equal(samples, 1);
const otherPeer = receiveRead({ ...request, peer: 'gateway-b', token: 'b8' });
assert.equal(otherPeer.token, 'b8');
assert.equal(samples, 2);
const fresh = receiveRead({ ...request, messageId: 4201, token: 'a8' });
assert.equal(fresh.messageId, 4201);
assert.equal(samples, 3);
console.log(JSON.stringify({ runtime: process.version, first, second, otherPeer, fresh, samples, checks: 6 }, null, 2));

Test the loss as a small in-memory fixture

The JavaScript fixture retains the first reply for a peer and a Message ID. The first acknowledgement is treated as lost. When the same peer retries with the same identifiers, the node returns the saved reply and the sample count stays at one. Another peer then uses 4200 and receives a response carrying its own Token. The initial sender uses 4201 for one more new read. Six assertions verify the reused response, the sample count, the distinct peer's Token, and the new request. This runs on a current Node runtime without opening a UDP socket. It models a narrow part of receiver behavior. Timing, message serialization, separate responses, and interoperability require checks against an actual CoAP implementation.

The duplicate record needs a bounded lifetime

A constrained device cannot remember every arrival indefinitely. A 16-bit Message ID can also be reused after a suitable interval. Its meaning is tied to a peer and an exchange window. RFC 7252 defines EXCHANGE_LIFETIME from transmission and latency assumptions that help bound exchange tracking. Change those assumptions only with awareness of the sender and receiver involved. Reusing the ID too early can cause a fresh request to be mistaken for a previous delivery. The toy program leaves its small table intact because its input consists of four arrivals. A device trial can record the ages of entries and table occupancy under expected traffic. That observation makes the cost of retry recognition measurable alongside packet loss.

An acknowledgement is distinct from the underlying work

Replace the simple read with a command directing the node to collect a longer sample. The same message may arrive twice while one physical sampling operation is underway. A message-layer duplicate record helps during the first window, but a restart can erase the record. If an operation reached the sensor immediately before power was lost, the sender may have seen no acknowledgement. A durable request or job record can tell the application what happened when the command's semantics need that distinction. Recording separate message and work identifiers makes those two timelines visible to the operator. An empty acknowledgement can close the delivery question while the application still waits for the sampling job to finish.

Give each observable event its own counter

A controlled trial can count distinct client requests, received packets, and physical sensor activations. Begin with straightforward delivery. Apply a test rule that discards the first acknowledgement. Then have two gateways use the same Message ID with different Tokens. The first retry increases packet count without increasing the sample count. The other gateway's request should produce its own sample. In a timestamped trace, record when the node took a reading and when the gateway delivered it to its caller. A repeated answer should keep the measurement time attached to the original sample. This ties message delivery and application activity together while preserving both useful observations.

843 reads ✎ Write a comment

Reader discussion

Discussion prompt

How would you measure this development's real-world impact?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.