Azure function running multiple times for the same service bus queue message

azure function send message to service bus topic
azure function service bus trigger batch
azure functions service bus sessions
azure functions queue trigger singleton
azure function service bus autocomplete
azure function host configuration
azure function batchsize
azure function aggregator

I have an Azure function (based on the new C# functions instead of the old .csx functions) that is triggered whenever a message comes into an Azure Service Bus Queue. Once the function is triggered, it starts processing the service bus message. It decodes the message, reads a bunch of databases, updates a bunch of others, etc... This can take upwards of 30 minutes at times.

Since, this is not a time sensitive process, 30 minutes or even 60 minutes is not an issue. The problem is that in the meanwhile, Azure Function seems to kick in again and picks up the same message again and again and reprocesses it. This is an issue and cause problems in our business logic.

So, the question is, can we force the Azure function to run in a singleton mode? Or if that's not possible, how do we change the polling interval?

The issue is related to Service Bus setting...

What is happening is that the message is added to the queue, the message is then given to the function and a lock is placed on that message so that no other consumer can see/process that message while you have a lock on it.

If within that lock period you do not tell service bus that you've processed the file, or to extend the lock, the lock is removed from the message and it will become visible to other services that will then process that message, which is what you are seeing.

Fortunately, Azure Functions can automatically renew the lock for you. In the host.json file there is an autoRenewTimeout setting that specifies for how long you want Azure Functions to keep on renewing the lock for.

https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json

"serviceBus": {
  // the maximum duration within which the message lock will be renewed automatically.
  "autoRenewTimeout": "00:05:00"
},

ServiceBusTrigger triggers multiple times the same message · Issue , ServiceBusTrigger triggers multiple times the same message #2796 seem to be able to accomplish with Azure Functions (Consumption plan). The function for the Service Bus Trigger problem is attached as source code. I tried using a queue (but you said you're seeing this for both SB and Queues). I am having two azure function one is http function and another one is queue trigger function. In http function i am adding a messasge to queue and in queue trigger processing it. When i log the message in queue trigger sometimes its triggering multiple times with the same message ? What will be the reason ? Thanks in advance

AutoRenewTimeout is not as great as suggested. It has a downside that you need to be aware of. It's not a guaranteed operation. Being a client side initiated operation, it can and sometimes will fail, leaving you in the same state as you are today.

What you could do to address it is to review your design. If you have a long running process, then you process the message, and hand off processing to something that can run longer than MaxLockDuration. The fact that your function is taking so long, indicates you have a long running process. Messaging is not designed for that.

One of the potential solutions would be to get a message, register processing intent in a storage table. Have another storage table triggered function to kick-off processing that could take X minutes. Mark it as a singleton. By doing so, you'll be processing your messages in parallel, writing "request for long running processing" into storage table, completing Service Bus messaging and therefore not triggering their re-processing. With the long running processing you can decide how to handle failure cases.

Hope that helps.

Same message process 2 times · Issue #2792 · Azure/azure , I have a function app which is working on Service Bus Topic binding, i am with function app trigger is processing the same message 2 times. the if there aren't some that are running longer than expected, leading to lock Logs are showing same function executed multiple times for the same message. The function for the Service Bus Trigger problem is attached as source code. It is dead simple. ServiceBusTrigger with a model, Input binding on a Blob, simulate processing (delay 7 minutes). Application insights is configured with the Function and it shows several times, the message about the trigger of the same message.

So your question is on how to make the Azure Function Message trigger to process one message at a time, and a message has to be processed once only.

I have been able to achieve the same functionality using the following host.json configuration.

{
  "serviceBus": {
    "maxConcurrentCalls": 1,
    "autoRenewTimeout": "23:59:59",
    "autoComplete": true
  }
}

Note that I set the autoRenewTimeout to 24 hour, which is long enough for a really long running process. Otherwise you can change it to a duration that fits your need.

Many will argue the suitability of using Azure Function for a long running operation. But that is not the question that needs an answer here.

c#: Azure function running multiple times for the same service bus , csx functions) that is triggered whenever a message comes into an Azure Service Bus Queue. Once the function is triggered, it starts processing  It is listening to an Azure Service Bus message queue. Nothing going on like Topics, just the most basic message queue. It is receiving/processing the same message multiple times, launching twice immediately when the message is received, then intermittently whilst the message is being processed.

I also experience the same issue, what I did is remove the default rule and add a custom rule on subscriber.

(OriginalTopic='name-of-topic' AND OriginalSubscription='name-of-subcription-sub') OR (NOT Exists([OriginalTopic]))

Azure Service Bus bindings for Azure Functions, Azure Functions integrates with Azure Service Bus via triggers and bindings. Run a function when a Service Bus queue or topic message is created NET class libraries while the extension bundle is used for all other  Azure Service Bus trigger for Azure Functions. 02/19/2020; 8 minutes to read; In this article. Use the Service Bus trigger to respond to messages from a Service Bus queue or topic.

Best practices for improving performance using Azure Service Bus , Describes how to use Service Bus to optimize performance when The term "​sender" is used for a Service Bus queue or topic client that sends messages to a Service Bus by reusing the same factory and client objects for multiple operations. If using a single client, set the prefetch count to 20 times the  If you need to pass larger messages between functions, an Azure Service Bus queue could be used to support message sizes up to 256 KB in the Standard tier, and up to 1 MB in the Premium tier. Service Bus topics are useful if you require message filtering before processing.

Windows Azure Data Storage, This differs from the Azure Service Bus, discussed in Chapter 5, which includes all In simple words, this means that messages don't just disappear. might be retrieved and processed multiple times from the same queue. the same result when a function or operation is performed multiple times against the same target. The duplicate detection time history defaults to 30 seconds for queues and topics, with a maximum value of seven days. You can change this setting in the queue and topic properties window in the Azure portal.

Triggers for Service Bus Queues should support multiple processes , Triggers for Service Bus Queues should support multiple processes at the same time. If I'm writing into a Queue that has a trigger to an azure function that works as a specify to such azure function how many times it should be run in parallel. if your function is not processing multiple messages in parallel. Functions 1.x. Functions 1.x apps automatically have a reference the Microsoft.Azure.WebJobs NuGet package, version 2.x. Next steps. Run a function when a Service Bus queue or topic message is created (Trigger) Send Azure Service Bus messages from Azure Functions (Output binding) Related Articles

Comments
  • Was your Function App created under the App Service Plan or Consumption Plan? Under App Service Plan, make sure that AlwaysOn is turned on. Under the Consumption Plan, the Function App is terminated after 5 minutes (configurable to extend to 10 minutes, see "functionTimeout" value at github.com/Azure/azure-webjobs-sdk-script/wiki/host.json), which may explain the behavior you are seeing.
  • I have the App Service Plan. My function is not terminating. In fact, the opposite is happening. Multiple functions are processing the message.
  • When you say "Mark it as a singleton", what is the it you are referring to? And how do I mark it as a singleton?
  • The other storage table triggered function. I've implemented somewhat similar process with NServiceBus and Service Bus. You'll find the code and the diagram here: docs.particular.net/samples/azure/… With functions approach, your long running Function (Processor) doesn't have to poll, it would be triggered.
  • I'd suggest using the github.com/Azure/azure-functions-durable-extension, which provides the long-running / orchestration capabilities you need
  • Matt, durable Functions are not there yet in terms of maturity.