DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

1

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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay