An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
Hello @LWB
Your trace contains the answer, and it's a reassuring one: are four strictly increasing sequence numbers with no gaps. IoT Hub emitted these in the correct order, and event 4 genuinely is the newest state — that device's true final state is connected, while your backend has recorded disconnected. The ordering signal was present in every event; the comparison was reading the wrong field. This is a one-field change, not a redesign.
Q: Why is the device showing disconnected in the backend when the latest event indicates connected?
A: The events in your trace have sequentially increasing sequenceNumber values with no gaps, which confirms they were generated in the correct order. The latest event indicates the device is connected, so the backend should reflect that state. This suggests the application is likely using the wrong field for event ordering.
Q: Which field should be used for event ordering?
A: Use sequenceNumber as the authoritative ordering field.
- It is the only field documented as strictly increasing.
- Compare it as a string, not as a numeric value.
- A higher sequenceNumber always represents a newer event.
Example:
bool isNewer = string.CompareOrdinal(incomingSeq, storedSeq) > 0;
Q: Can operationTimestamp be used to determine the latest event?
A: No.
operationTimestamp only indicates when the operation occurred. It is not guaranteed to increase sequentially and should not be used for state comparison or event ordering.
Q: Why can events appear out of timestamp order?
A: IoT Hub connection state events are generated from periodic 60-second snapshots, not from every individual connect or disconnect operation.
As a result:
- Newer events can contain older operation timestamps.
- Event delivery may be delayed or retried depending on the consumption path.
- This behavior is expected and does not indicate an issue with IoT Hub.
Q: What is the recommended implementation?
A: Maintain device state using sequenceNumber.
- Store the latest sequenceNumber per device.
- Accept an incoming event only if its sequenceNumber is greater than the stored value.
- Treat duplicate sequence numbers as no-ops.
- Use operationTimestamp for logging and diagnostics only.
Q: What should the final device state be in this scenario?
A: Since the captured events have increasing sequenceNumber values and the last event is connected, the correct final device state should be connected. If the backend still shows disconnected, the event ordering logic should be reviewed to ensure it uses sequenceNumber rather than operationTimestamp.