Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit concurrent Azure Function executions

I've seen this problem expressed a lot but I've yet to find a working solution.

In short, I periodically have a large batch of processing operations to be done. Each operation is handled by an Azure Function. Each operation makes calls to a database. If I have too many concurrent functions running at the same time, this overloads the database and I get timeout errors. So, I want to be able to limit the number of concurrent Azure Function calls that are run at a single time.

I've switched the function to be queue-triggered and tweaked the batchSize / newBatchThreshold / maxDequeueCount host.json settings in many ways based on what I've seen online. I've also set the WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT application setting to 1 in my function application settings to prevent more than one VM from being spawned.

Yet still, every time I fill that queue, multiple functions will spawn indiscriminately and my database will fall over.

How can I throttle the number of concurrent operations?

like image 619
vargonian Avatar asked Jan 01 '23 16:01

vargonian


1 Answers

The problem ended up being a difference in formatting of host.json in V1 and V2 Functions. Below is the correct configuration (using Microsoft.Azure.WebJobs.Extensions.Storage at least 3.0.1). The following host.json configures a single function app to process queue messages sequentially.

{ 
  "version":"2.0",
  "extensions": { 
    "queues": { 
      "batchSize": 1,
      "newBatchThreshold": 0 
     }
   } 
}

Setting App Setting WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1 restricts a function app from dynamically scaling out beyond one instance.

like image 187
Marie Hoeger Avatar answered Jan 18 '23 23:01

Marie Hoeger