@Bhanderi, Hareshkumar Babubhai HARBH we are sorry to hear that you're facing this issue with your Azure function app.
The EventGrid trigger implementation has changed between version 3.x and 4.x of Azure Functions. In version 4.x, the EventGrid trigger now uses a different approach to handle events, which can lead to potential issues with duplicates. EventGrid offers at-least-once delivery guarantees, which means that events may be delivered multiple times in certain scenarios. This behavior is not related to the Azure Functions runtime version, but it can be exacerbated by changes in the trigger implementation.
How did you migrate your app from version 3.x to version 4.x? your migrated .NET 6 code might not be using the built-in lease management in Azure Functions version 4.x. As a result, Event Grid might perceive delivery failures due to missing lease updates and keep retrying, leading to duplicate function invocations and database entries.
Here are some possible solutions that fix this issue:
- Instead of manual lease management, take advantage of the built-in lease functionality in Azure Functions version 4.x. It simplifies code and handles retries automatically. Refer to this Microsoft documentation for guidance on using Event Grid triggers with leases in version 4.x.
- If you're still using a custom lease management solution, carefully review your code to ensure its compatible with Azure Functions version 4.x behavior. Make sure your code properly updates or releases leases after successful function execution to signal successful event processing to Event Grid.
- In the Azure Functions 4.x runtime, you can enable function-level concurrency control to limit the number of concurrent executions of your function. This can help mitigate the issue of multiple instances processing the same event. To enable function-level concurrency control, go to the "Configuration" blade of your function app in the Azure portal, and add a new application setting with the key "FUNCTIONS_WORKER_PROCESS_COUNT" and the desired value (e.g., 1 to limit to a single instance).
- Depending on your use case, you may want to consider using alternative EventGrid delivery options, such as the "Delivery with Retry" policy, which can help mitigate the issue of duplicate events.
Hope that helps.
-Grace