Share via

What is device twin in Azure IoT Hub? — JSON state for device sync

Dhruvesh Sheladiya 0 Reputation points Microsoft Employee
2026-05-14T18:45:23.6433333+00:00

I am trying to understand the concept of device twins in Azure IoT Hub, specifically how they are used to maintain and synchronize device state between cloud applications and IoT devices using a JSON-based structure.


Environment Details

  • Azure Service: Azure IoT Hub
  • Feature: Device Twin
  • Data Format: JSON document
  • Communication Pattern: Cloud ↔ Device synchronization
  • Device State Management: Desired vs Reported state

Investigation Details / Observations

  • A device twin is a JSON document maintained by IoT Hub for each device. [learn.microsoft.com]
  • It stores device metadata, configuration, and runtime state. [learn.microsoft.com]
  • Device twins are automatically created when a device identity is registered. [learn.microsoft.com]
  • It acts as a cloud-side representation of the physical device. [oneuptime.com]
  • It enables state synchronization between device and backend apps, even when the device is offline. [oneuptime.com]

Core Components Observed

Tags

  • Metadata managed by backend apps only
    • Used for organizing and querying devices
      • Not visible to the device
      Desired Properties
      - Configuration/state defined by the cloud
      
         - Device reads and applies these settings
      
            - Example: telemetry interval, firmware version
      
            **Reported Properties**
      
               - Actual state reported by the device
      
                  - Backend reads this to monitor device status
      
                     - Example: battery level, current config
      

These properties work together to synchronize expected state vs actual state. [learn.microsoft.com]


Steps Followed / Investigation Done

  • Reviewed IoT Hub documentation on device twins
  • Explored device twin JSON structure
  • Analyzed how desired and reported properties interact
  • Observed lifecycle tied to device identity
  • Checked how backend and device apps use the twin

Mitigation / Considerations Explored

  • Using device twins for remote configuration of devices
  • Leveraging reported properties for monitoring device health/state
  • Using tags for querying and grouping large device fleets
  • Considering offline scenarios where device sync happens later

Challenges / Confusions

  • Difference between device twin vs direct messaging
  • How conflicts are resolved between desired and reported states
  • How often synchronization occurs between cloud and device
  • Limitations on size and frequency of updates

Ask / Clarification Needed

  • What is the best way to design desired vs reported properties?
  • How does IoT Hub handle synchronization when devices are offline?
  • What are best practices for using device twins at scale?I am trying to understand the concept of device twins in Azure IoT Hub, specifically how they are used to maintain and synchronize device state between cloud applications and IoT devices using a JSON-based structure.

    Environment Details

    • Azure Service: Azure IoT Hub
      • Feature: Device Twin
        • Data Format: JSON document
          • Communication Pattern: Cloud ↔ Device synchronization
            • Device State Management: Desired vs Reported state

    Investigation Details / Observations

    • A device twin is a JSON document maintained by IoT Hub for each device. [learn.microsoft.com]
      • It stores device metadata, configuration, and runtime state. [learn.microsoft.com]
        • Device twins are automatically created when a device identity is registered. [learn.microsoft.com]
          • It acts as a cloud-side representation of the physical device. [oneuptime.com]
            • It enables state synchronization between device and backend apps, even when the device is offline. [oneuptime.com]

    Core Components Observed

    Tags
         - Metadata managed by backend apps only
    
               - Used for organizing and querying devices
    
                     - Not visible to the device
    
                        **Desired Properties**
    
                              - Configuration/state defined by the cloud
    
                                    - Device reads and applies these settings
    
                                          - Example: telemetry interval, firmware version
    
                                             **Reported Properties**
    
                                                   - Actual state reported by the device
    
                                                         - Backend reads this to monitor device status
    
                                                               - Example: battery level, current config
    
    These properties work together to synchronize expected state vs actual state. [learn.microsoft.com]

    Steps Followed / Investigation Done

    • Reviewed IoT Hub documentation on device twins
      • Explored device twin JSON structure
        • Analyzed how desired and reported properties interact
          • Observed lifecycle tied to device identity
            • Checked how backend and device apps use the twin

    Mitigation / Considerations Explored

    • Using device twins for remote configuration of devices
      • Leveraging reported properties for monitoring device health/state
        • Using tags for querying and grouping large device fleets
          • Considering offline scenarios where device sync happens later

    Challenges / Confusions

    • Difference between device twin vs direct messaging
      • How conflicts are resolved between desired and reported states
        • How often synchronization occurs between cloud and device
          • Limitations on size and frequency of updates

    Ask / Clarification Needed

    • What is the best way to design desired vs reported properties?
    • How does IoT Hub handle synchronization when devices are offline?
    • What are best practices for using device twins at scale?
Azure IoT Hub
Azure IoT Hub

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


1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-05-14T18:45:58.5866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    A device twin in Azure IoT Hub is a JSON document that IoT Hub maintains for each device identity. It stores device-related information such as metadata, configuration, and state, and is used by device apps and back-end apps to synchronize device conditions and configuration and to query and target long-running operations.

    Key characteristics:

    • Automatically created and deleted with the device identity in the IoT Hub identity registry.
    • Used to store and sync:
      • Device-specific metadata (for example, location, device type).
      • Configuration and runtime state (for example, connectivity method, firmware rollout state).
    • Enables back-end apps to manage devices even when they are offline; the device reconciles state when it reconnects.

    Structure of a device twin JSON document:

    1. Device identity properties (root)
      • Read-only properties that mirror the corresponding device identity in the identity registry.
    2. Tags
      • Cloud-only metadata.
      • Read/write by back-end apps; not visible to device apps.
      • Used to organize and query devices (for example, by location, function, or device type).
    3. Desired properties
      • Set by back-end apps to express the desired configuration or state.
      • Read by device apps; device apps can subscribe to change notifications.
      • Used together with reported properties to synchronize configuration or conditions (for example, desired firmware version, telemetry interval).
    4. Reported properties
      • Set by device apps to report current configuration or state.
      • Read and queried by back-end apps.
      • Used together with desired properties to track progress and actual state (for example, current firmware version, battery level, connectivity method).
    5. Metadata and versioning
      • Desired and reported properties include $version values that are guaranteed to be incremental.
      • The twin has an etag for optimistic concurrency.

    Synchronization and offline behavior:

    • Device twins support optimistic concurrency using etag on the twin and $version on desired/reported properties.
    • IoT Hub does not preserve desired property update notifications for disconnected devices.
    • Recommended reconnection flow for a device app:
      1. Connect to IoT Hub.
      2. Subscribe for desired property update notifications.
      3. Retrieve the full desired properties document.
    • The device can ignore notifications whose $version is less than or equal to the version of the retrieved document, because versions always increment.
    • This pattern ensures that devices converge to the latest desired configuration after being offline.

    Designing desired vs reported properties:

    • Desired properties:
      • Model the configuration or target state that the back end wants devices to reach (for example, desired.telemetryInterval, desired.firmware.version).
      • Back-end apps update desired properties; devices observe and apply them.
    • Reported properties:
      • Model the actual state and capabilities of the device (for example, reported.telemetryInterval, reported.firmware.version, reported.batteryLevel).
      • Devices update reported properties; back-end apps read and query them.
    • Together they allow the back end to compare expected vs actual state and drive workflows such as configuration rollout or firmware updates.

    Using device twins at scale:

    • Tags and reported properties can be queried by back-end apps to select subsets of devices (for example, all devices in a region with a specific capability or state) and to target long-running operations.
    • Optimistic concurrency with etag and $version should be used to ensure consistent updates when multiple actors update twins.

    Programming model:

    • Device apps and service (back-end) apps use Azure IoT SDKs (.NET, Node.js, Python, Java) to:
      • Device apps: handle desired property updates and update reported properties.
      • Service apps: update tags and desired properties and query devices based on twin values.

    These behaviors and patterns are available only in the standard tier of IoT Hub.


    References:

    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.