DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

C#: Retry mechanism for Event Hub Trigger Function

We recently used an Event Hub trigger for Azure Functions and realized how to handles retry when the function code throws an exception.

Default Behavior

Even though we throw an exception, the function app considers the job is done, and update checkpoint. This means we cannot process the event again.

Add Retry Policy

Azure Functions error handling and retries explains how we can add the retry policy in case of failure.

  • Fixed Delay: Retry after certain amount of time
  • Exponential back-off: It keep increasing the wait time for the next try

Exponential back-off is a good way if we want to avoid retry too frequently when there is a issue which requires some time to be fixed.

Code Sample

The following code sample is taken from the office doc. It's quite easy as we just need to add an appropriate attribute with arguments. Following example retries 5 times in total with 4 seconds as the minimum wait time, and 15 minutes as the maximum wait time.

If we specify -1 as retry count, it retires indefinitely.

[FunctionName("EventHubTrigger")]
[ExponentialBackoffRetry(5, "00:00:04", "00:15:00")]
public static async Task Run([EventHubTrigger("myHub", Connection = "EventHubConnection")] EventData[] events, ILogger log)
{
// ...
}
Enter fullscreen mode Exit fullscreen mode

When we specify -1 as retry count, we need to make sure to implement alert system so that someone is aware of the issue to fix the cause!

Summary

At the beginning, I thought the Function App will retry automatically in case of the failure, but it doesn't. I hope this article helps me in the future.

Top comments (0)