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.

đź‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay