I have an external API which invokes my HTTP trigger azure function with the same query parameters 5 times at the same moment. So 5 requests are processed in the same time concurrently, each request adds a record to my google sheet and it causes unwanted duplicated records. My function is checking for duplicate in that sheet before pushing new record but when 5 instances are called a the same time concurrently, duplicate does not exist. Is there any simple solution to achieve processing those 5 request one by one, without concurrency?
-
Write to a database instead of google sheet and merge the data based on a key. – Nick.Mc Nov 27 '22 at 11:46
-
Introduce a queue? – Peter Bons Nov 27 '22 at 13:04
-
@PeterBons I tried the Azure queue but it behaves the same way concurrently. – Hawos Nov 27 '22 at 13:29
-
@Nick.McDermaid I can't do this with db and that's not my decision – Hawos Nov 27 '22 at 13:30
-
Is there anyone else who know how to ensure non concurrency on the http function or maybe queue? – Hawos Nov 27 '22 at 13:31
-
2When using a queue you need to tell the runtime to use a single instance. See https://stackoverflow.com/a/70701491/932728 – Peter Bons Nov 27 '22 at 16:19
1 Answers
I have same trouble here but with CosmosDB. If I make x times same request at same time on Azure Function it just query for a property in Cosmos, but it already doesn't exists and duplicate the insertion but with different ID.
I'm looking for a solution, but the best I anchieved until now is to use [Singleton] decorator on .Net environment. It makes the function finish a request before start another one.
[Singleton]
public async Task<IActionResult> FunctionName(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "functionRoute")] HttpRequest req)
{
//your code here
}
If anyone have another solution, please post here.
Some more information about my duplicated data:
- They have the same _ts (timestamp) on Cosmos;
- Watching to a Date propertie from the created objects, the difference between they are about milliseconds;
- Watching to ApplicationInsigths the function Excuted message are at the same time hh:mm:ss;
Edit:
Today I found that: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#scope-values
and make something like this:
[Singleton("{anyParameter}")]
[OpenApiParameter(name: "anyParameter", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "anyParameter")]
public async Task<IActionResult> FunctionName(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "functionRoute")] HttpRequest req)
{
//your code here
}
It worked to me. If there is a Function instance running with same "anyParameter" parameter it executes like an queue. If the parameters are different it create another instace and run on another Function Instance;
Watching to Application Insights it shows that requisitions with the same "anyParameter" execute like in queue, and duration time of function Increase for every request. But with another value to "anyParameter" it really creates another instance and the requisitions are being completed at the same speed time.

- 11
- 2