Azure IoT Hub device queries returns stale data

Stian Sandve 20 Reputation points
2025-11-17T10:20:27.77+00:00

We are using Azure IoT Hub and have integrations running on dotnet using the C# SDK. Lately we have been experiencing issues where the GetNextAsTwinAsync returns old versions of the device twin for some devices. It is not a matter of eventual consistency, because the returned twins are several days old. When I fetch the twin directly using GetTwinAsync, I get the most recent twin.

I have isolated it to the Azure IoT Hub API, because I experience the same from the Azure Portal or if I use the /query API using Postman.

Example:

POST https://our-iot-hub.net/devices/query?api-version=2024-03-31

BODY

{"query":"SELECT * FROM devices WHERE deviceId = 'FOOBAR'"}

RESPONSE

[
	{
		"deviceId": "FOOBAR",
		"etag": "AAAAAAAAAHU=",
		"deviceEtag": "NTI5MjY0NzMy",
		"status": "enabled",
		"statusUpdateTime": "0001-01-01T00:00:00Z",
		"connectionState": "Connected",
		"lastActivityTime": "2025-11-12T19:33:32.2807186Z",
		"cloudToDeviceMessageCount": 0,
		"authenticationType": "sas",
		"x509Thumbprint": {
			"primaryThumbprint": null,
			"secondaryThumbprint": null
		},
		"modelId": "",
		"version": 5031,
...

When I fetch the twin directly by device ID, I get the most recent version:

GET https://out-iot-hub.net/twins/FOOBAR?api-version=2024-03-31

RESPONSE

{
	"etag": "AAAAAAAAAMA=",
	"deviceId": "FOOBAR",
	"deviceEtag": "NTI5MjY0NzQw",
    "status": "enabled",
	"statusUpdateTime": "0001-01-01T00:00:00.0000000Z",
	"lastActivityTime": "2025-11-17T10:00:02.1081882Z",
	"connectionState": "Connected",
	"version": 5335,
...

I am seeing the same results with api version 2021-04-12, which is what the SDK uses.This is a major issue for us, and causes all kinds of bugs related to outdated desired/reported properties. Just to be clear, we are not having issues with direct twin reads and writes. It works as expected. The problem is that device queries operate on very old versions of the device twin for some of our devices.

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
{count} votes

Answer accepted by question author
  1. SRILAKSHMI C 10,640 Reputation points Microsoft External Staff Moderator
    2025-11-17T17:12:05.76+00:00

    Hello Stian Sandve,

    Welcome to Microsoft Q&A and Thank you for reaching out.

    I understand that you’re experiencing an issue in Azure IoT Hub where GetNextAsTwinAsync (query-based twin retrieval) is returning outdated device twins, while GetTwinAsync (direct twin read) always returns the latest state. This mismatch can understandably create bugs and inconsistent behavior in your application.

    Based on your testing through the SDK, REST API, and Azure Portal, this behavior is not expected. Device twin queries should return near-real-time data not versions that are several days old.

    This issue occurs due to a known limitation in how IoT Hub handles device queries versus direct reads. Direct twin reads (GetTwinAsync) always fetch the latest live state from the service. However, query operations (/devices/query or GetNextAsTwinAsync) rely on an internal indexed store that is updated asynchronously. Normally, this index refreshes within seconds or minutes, but in your case the index appears stale for days, which is abnormal and indicates a backend issue.

    The root cause is typically tied to indexing delays or failures within IoT Hub. The internal index may stop updating for some devices if the service is experiencing throttling, high ingestion rates, or a stuck update pipeline. In many cases, this issue occurs at the partition level IoT Hub stores devices across partitions, and if one partition's indexer fails, only those devices return stale data via queries.

    Changing API versions (e.g., 2021-04-12 vs 2024-03-31) won’t resolve the issue because the underlying query-indexing mechanism is the same across all API versions.

    To diagnose the issue, comparing the version from query results with direct reads as you’ve done is the correct first step.

    If the difference is large (multiple days), this is not standard eventual consistency and clearly points to an indexing problem. You can also review IoT Hub metrics in Azure Monitor, such as TwinUpdateOperations, QueryOperations, throttling events, and errors.

    Running queries for multiple devices that belong to the same partition can also confirm if the problem is isolated to a specific partition.

    As a temporary workaround, avoid relying on queries for real-time or fast-changing twin data. Instead, use GetTwinAsync wherever accurate and latest state is required.

    If you need to perform batch or large-scale processing, consider using device twin change notifications (Event Grid or routes) to maintain your own up-to-date cache, and reserve queries for static or slowly changing metadata such as tags or deviceId lists.

    Please refer this

    IoT Hub query language for device and module twins

    Issues with twin reads or updates

    Common error codes

    I Hope this helps. Do let me know if you have any further queries.

    Thank you!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Stian Sandve 20 Reputation points
    2025-11-17T21:15:44.0666667+00:00

    First of all, thanks for the fast response! I was not expecting you to reply that fast, so I already started on a workaround. I actually went for what you suggested and added a route for device twin updates that sends messages to a service bus topic that I subscribe to. I use this, in combination with a period full sync to keep a copy of all device twins in Cosmos DB. I think this will work well, but it sure did cost me valuable time to implement this workaround. It also adds operational / infrastructure cost.

    I browsed the IoT Hub logs and found no evidence of throttling. I also tried to upgrade from S1 to S2 to see if that would trigger a new indexing process, but the index was still broken. There is no way for me to manually "restart" or "clear cache" on the IoT Hub service, which I understand. It does however feel like your hands are tied when problems like this arise on a managed service. You definitely loose faith, which is why I had to go with the separate database solution...


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.