Azure Iot Hub device connected/disconnected event processing order

LWB 0 Reputation points
2026-08-18T09:45:51.99+00:00

Hello,

I'm looking forward to obtaining some guidance on how to properly handle device connected and disconnected events. My main concern is:

  • Ordering - How to ensure that our backend system always has the most updated version of the "connected"/"disconnected" status.

Our logic was built on a simple assumption: We only accept events with OperationTimestam > last processed event timestamp.

The logic described was solid, until recently when we saw an increased number of devices with the following behaviour:

0 - Device connected normally. 
1 - disconnected event  | OperationTimestamp: Aug 17, 2026 @ 14:09:34.214                  Sequence Number: 000000000000000001DD1E0F0EAC4F200000005600000000000000000000082B 
2 - connected event     | OperationTimestamp: Aug 17, 2026 @ 14:12:38.567                  Sequence Number: 000000000000000001DD1E0F0EAC4F200000005600000000000000000000082C 
3 - disconnected event  | OperationTimestamp: Aug 17, 2026 @ 14:14:09.913                  Sequence Number: 000000000000000001DD1E0F0EAC4F200000005600000000000000000000082D 
4 - connected event     | OperationTimestamp: Aug 17, 2026 @ 14:12:38.568 (Event dropped)  Sequence Number: 000000000000000001DD1E0F0EAC4F200000005600000000000000000000082E

The point here is that events 3 and 4 have consecutive sequence numbers (...082D and ...082E), but event 4 has an OperationTimestamp approximately 1 minute and 31 seconds older than event 3. Because our current processing logic uses OperationTimestamp to reject older events, event 4 is discarded even though its sequence number indicates that it follows event 3.

This raises the following questions:

  1. Should SequenceNumber be considered the authoritative property for determining event ordering, or are there other properties/guarantees we should use?
  2. What exactly does OperationTimestamp represent?
  3. Is OperationTimestamp guaranteed to be increasing for events generated by the same device?
  4. If not, under what circumstances can an event have an OperationTimestamp older than an event that has already been processed?
  5. What is the recommended approach for ensuring that our backend always applies the latest device connection state when OperationTimestamp and SequenceNumber indicate different event orderings?
  6. How does SequenceNumber behave under failover circumstances? Does it continue to be sequential or do we need to take any actions from our side?

We would appreciate any documentation or guidance on the expected ordering guarantees and the recommended event-processing strategy.

Azure IoT Hub
Azure IoT Hub

An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Manish Deshpande 7,975 Reputation points Microsoft External Staff Moderator
    2026-08-18T22:58:47.2266667+00:00

    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.

    1. Store the latest sequenceNumber per device.
    2. Accept an incoming event only if its sequenceNumber is greater than the stored value.
    3. Treat duplicate sequence numbers as no-ops.
    4. 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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.