How to use exponential back-off logic in Azure device client?

DEEPAK KUMPALA 191 Reputation points
2022-09-15T07:05:24.097+00:00

I have a job in C# which runs every 5 minutes and acts like a heartbeat to check the client status

When the status is Disconnected_Retrying, I exit the timer job assuming retry will work. However, status always comes as Disconnected_Retrying and leads to unnecessary calls/request to server every 5 minute. Hence I am thinking of implementing exponential backoff method to overcome this issue

Started reading below GitHib code https://github.com/Azure/azure-iot-sdk-csharp/blob/main/iothub/device/src/RetryPolicies/ExponentialBackoff.cs

But I am not able to understand how to implement it.

Note: I have created same question in stack overflow for better reach

Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
228 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,419 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 31,391 Reputation points
    2025-04-16T14:41:48.47+00:00

    Hello Deepak !

    Thank you for posting on Microsoft Learn.

    The Azure IoT SDK for C# already supports retry policies, including exponential back-off, and you can apply them directly to your DeviceClient instance.

    Add the required namespaces :

    using Microsoft.Azure.Devices.Client; 
    using Microsoft.Azure.Devices.Client.Retry;
    

    Then, create the DeviceClient with an ExponentialBackoff RetryPolicy.

    You need to instantiate the DeviceClient with exponential back-off:

    TimeSpan minBackoff = TimeSpan.FromSeconds(1);
    TimeSpan maxBackoff = TimeSpan.FromSeconds(60);
    TimeSpan deltaBackoff = TimeSpan.FromSeconds(5);
    int maxRetryCount = 5;
    
    RetryPolicy retryPolicy = new ExponentialBackoff(maxRetryCount, minBackoff, maxBackoff, deltaBackoff);
    
    DeviceClient deviceClient = DeviceClient.CreateFromConnectionString("YourConnectionString", TransportType.Mqtt);
    deviceClient.SetRetryPolicy(retryPolicy);
    
    • minBackoff: Minimum backoff duration between retries

    maxBackoff: Maximum cap on the retry interval

    deltaBackoff: Added randomness (jitter)

    maxRetryCount: How many retries before giving up

    You can monitor connection status (optional, for logging/debugging) :

    deviceClient.SetConnectionStatusChangesHandler((status, reason) =>
    {
        Console.WriteLine($"Connection status: {status}, Reason: {reason}");
    });
    

    Now that retries are handled by the SDK using exponential back-off, your timer logic should check if the client is Connected before doing work, and do nothing if it's retrying:

    var status = deviceClient.GetConnectionStatus();
    if (status == ConnectionStatus.Connected)
    {
        // Do your work
    }
    else
    {
        // Let the SDK handle retries; don't do anything here
        Console.WriteLine("Device is retrying connection, skipping this cycle.");
    }
    
    
    0 comments No comments

Your answer

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